C# VS2010 SQLSERVER2008 在数据库中查询信息是报错 找不到表0
代码如下:namespaceWindowsFormsApp_Star{publicpartialclassForm8:Form{stringconnString;SqlC...
代码如下:
namespace WindowsFormsApp_Star
{
public partial class Form8 : Form
{
string connString;
SqlConnection connection;
public Form8()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connString = @"Data Source=TOSHIBA-PC;Initial Catalog=管理系统;Integrated Security=True";
connection = new SqlConnection(connString);
string name = textBox1.Text;
string sql=String.Format("select * from Star where Sname like '%0%'",name);
try
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet("Star");
dataGridView1.Columns[0].DataPropertyName = "Sname";
dataGridView1.Columns[1].DataPropertyName = "Sex";
dataGridView1.Columns[2].DataPropertyName = "Nationality";
dataGridView1.Columns[3].DataPropertyName = "Birth";
dataGridView1.Columns[4].DataPropertyName = "Works";
dataGridView1.DataSource = dataSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
} 展开
namespace WindowsFormsApp_Star
{
public partial class Form8 : Form
{
string connString;
SqlConnection connection;
public Form8()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connString = @"Data Source=TOSHIBA-PC;Initial Catalog=管理系统;Integrated Security=True";
connection = new SqlConnection(connString);
string name = textBox1.Text;
string sql=String.Format("select * from Star where Sname like '%0%'",name);
try
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet("Star");
dataGridView1.Columns[0].DataPropertyName = "Sname";
dataGridView1.Columns[1].DataPropertyName = "Sex";
dataGridView1.Columns[2].DataPropertyName = "Nationality";
dataGridView1.Columns[3].DataPropertyName = "Birth";
dataGridView1.Columns[4].DataPropertyName = "Works";
dataGridView1.DataSource = dataSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
} 展开
2个回答
展开全部
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet("岁灶Star");
这有问题
你没有把数据集填充哗雀如到乱启dataset
加一句
dataAdapter .Fill(dataSet ,"dataSet");
DataSet dataSet = new DataSet("岁灶Star");
这有问题
你没有把数据集填充哗雀如到乱启dataset
加一句
dataAdapter .Fill(dataSet ,"dataSet");
追问
不报错了 但是查询的内容显示不了。。
追答
dataGridView1.Columns[0].DataPropertyName = "Sname";
dataGridView1.Columns[1].DataPropertyName = "Sex";
dataGridView1.Columns[2].DataPropertyName = "Nationality";
dataGridView1.Columns[3].DataPropertyName = "Birth";
dataGridView1.Columns[4].DataPropertyName = "Works";
你要确保dataGridView绑定的字段 与你数据表的字段一样
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询