如何从Comp表中获取company_name的值并将其存储在comboBox中?
这是我从数据库获取值并将其存储在组合框中的初始代码:
string Sql = "select company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
Run Code Online (Sandbox Code Playgroud)
它指出da.fill(ds)并说"Could not locate entry in sysdatabases for database 'select company_name from JO'.找不到具有该名称的条目.确保输入的名称正确."
希望你的回复谢谢!
当我运行这段代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说“不能在此范围内声明名为 'e' 的局部变量,因为它会给 ' 赋予不同的含义e',它已经在“父级或当前”范围中使用来表示其他内容”
try
{
//Form Query which will insert Company and will output generated id
myCommand.CommandText = "Insert into Comp(company_name) Output Inserted.ID VALUES (@company_name)";
myCommand.Parameters.AddWithValue("@company_name", txtCompName);
int companyId = Convert.ToInt32(myCommand.ExecuteScalar());
//For the next scenario, in case you need to execute another command do it before committing the transaction
myTrans.Commit();
//Output Message in message box
MessageBox.Show("Added", "Company Added with id" + companyId, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException …Run Code Online (Sandbox Code Playgroud)