using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace demo37
{
public partial class Form1 : Form
{
SqlConnection conn;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection("server=.;database=test3;uid=sa;pwd=123456");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from " + textBox1.Text.Trim(), conn);
SqlDataReader sqr = cmd.ExecuteReader();
sqr.Read();
if (sqr.HasRows)
{
MessageBox.Show("資料表中有資料");
}
else
{
MessageBox.Show("資料表中沒有資料", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
//conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;database=test3;uid=sa;pwd=123456");
SqlCommand cmd = new SqlCommand("select * from test3", conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "test3");
dataGridView1.DataSource = ds.Tables[0];
}
}
}
uj5u.com熱心網友回復:
select * from test3 ,沒test3這個表uj5u.com熱心網友回復:
建議你先除錯代碼uj5u.com熱心網友回復:
已經弄好了,我把test3輸錯了,應該是Table_3uj5u.com熱心網友回復:
MessageBox彈出了嗎uj5u.com熱心網友回復:
可以彈出來的uj5u.com熱心網友回復:
test3是資料庫名啊,你要查的表應該是Table_3吧轉載請註明出處,本文鏈接:https://www.uj5u.com/net/82420.html
標籤:C#
