0

问题

我已经为此苦苦挣扎了一段时间。虽然我认为我已经初始化了 select 命令,但我有以下错误?

dataadapter.selectcommand.connection 属性需要初始化

我知道由于没有设置主键,我没有收到此错误,因为我已阅读这可能是问题所在。我肯定有一个主键。

背景

此函数选择使用 if 语句调用的查询。在查询中有参数化变量,这些变量是根据最终用户在两个组合框中选择的内容来选择的。

SQLSelection();

示例查询

   SQL = "SELECT * FROM dbo.joblist_TEST WHERE username = @username and status in ('New','Hold')";

不工作的位是Update_Click事件句柄。

代码

 public partial class Form1 : Form
    {
        int weeks = 0;
        SqlCommand command = new SqlCommand();
        SqlDataAdapter adb = new SqlDataAdapter();
        SqlDataAdapter adapter = new SqlDataAdapter();
        SqlCommandBuilder builder = new SqlCommandBuilder();
        SqlCommand selectCommand = new SqlCommand();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection();

        String ConnString = "Data Source=sqlexpress; Initial Catalog=MobileData; User ID=mobile; Password=pw";
}


public DataTable getDataTable()
            {

            //Decide what query
            String SQL = SQLSelection();

            // SqlDbConnection con = new OleDbConnection(ConnString);
            SqlConnection con = new SqlConnection(ConnString);

            //open connection to database
            con.Open();

            //create adapter that sits inbetween dataset and datbase
            SqlDataAdapter adapter = new SqlDataAdapter();


            adapter.SelectCommand = new SqlCommand(SQL, con);
            adapter.UpdateCommand = new SqlCommand(SQL, con);

            adapter.SelectCommand.Parameters.Add("@username", SqlDbType.VarChar).Value = auditorCmb.Text;
            adapter.SelectCommand.Parameters.Add("@status", SqlDbType.VarChar).Value = statusCmb.Text;

            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);


            adapter.Fill(ds, "jobList_Test");
            dt = ds.Tables["jobList_Test"];


            dataGridView1.DataSource = ds.Tables["jobList_Test"];

            int rowCount = rowCount = dt.Rows.Count;
            label10.Text = rowCount.ToString("n0");


            return dt;
        }

        public void Update_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Are you sure you want to save changes?", "Save Changes", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                try
                {


                    adapter.SelectCommand = command; // cmd1 is your SELECT command

                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

                    builder.GetUpdateCommand(); // Force the building of commands
                    adapter.Update(ds, "jobList_Test");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());

                }
            }




        }
4

3 回答 3

2

问题是您没有设置您刚刚设置的选择命令 adapter.SelectCommand = command; // cmd1 是您的 SELECT 命令,我认为您没有定义什么命令只是新的 sqlCommand ...请提供有关您要做什么的更多信息,以便我可以提供更多帮助。

于 2015-03-29T22:16:15.313 回答
2

您的此代码adapter.SelectCommand = command;正在使用一个 SqlCommand,该 SqlCommand 仅在没有指定连接的情况下使用 SqlCommand 进行初始化。我觉得你做事也过头了。提及您想要实现的目标可能有助于获得更好的答案。

于 2015-03-29T22:17:34.663 回答
1

您需要稍微简化示例代码。我什至看不到在哪里getDataTable()被调用。

无论哪种方式,你都会走上一条以失败而闻名的道路。SqlConnectionandSqlCommand对象不应该实例化为全局变量。相反,它们应该在调用代码块中创建和处理。

为什么?因为它们实现 IDisposable 并处理非托管资源。这意味着如果/当您的程序出现问题时,您将泄漏内存和数据库连接。

请注意,即使每秒创建和处理数千个 SqlConnection 或 SqlCommand 对象也不会导致性能问题。

最终,看起来你把事情复杂化了。

于 2015-03-29T22:20:26.450 回答