我有datagridview,我必须填写5个表。我声明了SqlCommand和SqlConnection。 之后我会使用这样的东西:
let
因此,我在datagridview中有我的查询的列标题,但没有数据。 我尝试使用这段代码:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = selCommand;
ad.Fill(dt);
dataGridView1.DataSource = dt;
它正在运作,但我发生了一些变化,我无法理解为什么它不起作用。
答案 0 :(得分:0)
试试这个:
DataTable table = null;
using (SqlConnection connection = new SqlConnection(this.connectionString))
{
try
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM Something WHERE Id = @Id";
cmd.Parameters.Add(new SqlParameter("@Id", YourValue));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
table = new DataTable();
adapter.Fill(table);
}
}
catch (Exception ex)
{
//Handle your exception;
}
}
dataGridView1.DataSource = table;