ExecuteReader: Connection property has not been initialized

前端 未结 7 1417
广开言路
广开言路 2021-01-01 08:29

ExecuteReader: Connection property has not been initialized.

my coding is

protected void Button2_Click(object sender,          


        
相关标签:
7条回答
  • 2021-01-01 09:32

    As mentioned you should assign the connection and you should preferably also use sql parameters instead, so your command assignment would read:

        // 3. Pass the connection to a command object
        SqlCommand cmd=new SqlCommand ("insert into time(project,iteration) values (@project, @iteration)", conn); // ", conn)" added
        cmd.Parameters.Add("project", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
        cmd.Parameters.Add("iteration", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
    
        //
        // 4. Use the connection
        //
    

    By using parameters you avoid SQL injection and other problematic typos (project names like "myproject's" is an example).

    0 讨论(0)
提交回复
热议问题