executenonquery:连接尚未初始化 [英] executenonquery:connection has not been intialized

查看:88
本文介绍了executenonquery:连接尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解决此问题

please solve this problem

using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient; 
using System.Text;
using System.Windows.Forms;

namespace empinformation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        int currenrec = 0;
        int totalrec = 0;
       static SqlConnection con = new SqlConnection("user id=sa; password=123;database=work");

       static SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        private void button1_Click(object sender, EventArgs e)
        {
            
            da.Fill(ds, "emp");
            dt = ds.Tables["emp"];
            currenrec = 0;
            totalrec = dt.Rows.Count;
            FillControls();
            btnext.Enabled = true;
            btprev.Enabled = true;
        }
        private void FillControls()
        {
            TextBox1.Text = dt.Rows[currenrec]["empno"].ToString();
            TextBox2.Text = dt.Rows[currenrec]["ename"].ToString();
            TextBox3.Text = dt.Rows[currenrec]["job"].ToString();
            TextBox4.Text = dt.Rows[currenrec]["sal"].ToString();
            TextBox5.Text = dt.Rows[currenrec]["hiredate"].ToString();
           TextBox6.Text = dt.Rows[currenrec]["comm"].ToString();
           TextBox7.Text = dt.Rows[currenrec]["deptno"].ToString();
        }

        private void btnext_Click(object sender, EventArgs e)
        {
            currenrec += 1;
            if (currenrec >= totalrec)
            {
                currenrec = 0;
            }
            FillControls();
        }

        private void btprev_Click(object sender, EventArgs e)
        {

            currenrec -= 1;
            if (currenrec <= totalrec)
            {
                currenrec = totalrec-1;
            }
            FillControls();
        }

        private void btnew_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno) ";
            cmd.Parameters.AddWithValue("@empno",TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename",TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept",TextBox7.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

推荐答案

将连接对象分配给cmd

cmd.Connection = con;

参见下面的代码

assign connection object to cmd

cmd.Connection = con;

see below code

cmd.CommandText = "insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno) ";
            cmd.Parameters.AddWithValue("@empno", TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename", TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept", TextBox7.Text);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();



在查询和命令参数中也使用相同的参数

cmd.Parameters.AddWithValue("@ sal",TextBox4.Text);

希望这对您有帮助



Also use same parameter in query and in command parameter

cmd.Parameters.AddWithValue("@sal", TextBox4.Text);

Hope this will help you


请在插入查询时提供连接对象.


please give connection object while your insert query.


private void btnew_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = ("insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno)",con);
            cmd.Parameters.AddWithValue("@empno",TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename",TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept",TextBox7.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
        }




希望这会帮助您,如果不能,请发布.




hope this will hep you, if not please Post it.


这篇关于executenonquery:连接尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆