tis连接有什么问题 [英] What is problem with tis connection

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

问题描述

 SqlConnection con =  new  SqlConnection(); 
con.ConnectionString = Server =(Local); Database = final; Integrated Security = True;
con.Open();
SqlCommand cmd = new SqlCommand( SELECT * FROM [zarfiat] WHERE shahr = @ shahr AND mantaghe = @ mantaghe,con);
cmd.Parameters.AddWithValue( shahr,textBox5.Text);
cmd.Parameters.AddWithValue( mantaghe,textBox6.Text);
SqlDataReader dr = null ;
dr = cmd.ExecuteReader();
while (dr.Read())
{
zarfiat = Convert.ToInt32(dr [ zarfiat]);
如果(zarfiat > 0
{
if (textBox1.Text!= & textBox2.Text!= & textBox3.Text!= & textBox4.Text!= & textBox5.Text!= & textBox6.Text!=
{
cmd = new SqlCommand( INSERT INTO [info](kod,姓名,家庭,电话,shahr,mantaghe) VALUES(' + textBox1.Text + ',' + textBox2.Text + ',' + textBox3.Text + ',' + textBox4.Text + ',' + textBox5.Text + ',' + textBox6 .Text + '));
cmd.ExecuteNonQuery();
}
else label8.Text = پرکردنتمامیاطلاعاتالزامیمیباشد;
}
else label8.Text = ظرفیتخالیدرمکانموردنظرموجودنمیباشد;
}
zarfiat--;
cmd = new SqlCommand( UPDATE [最终] SET zarfiat =' + zarfiat + ');
cmd.ExecuteNonQuery();
label8.Text = مشترکموردنظرباموفقیتثبتشد;
con.Close()









此错误出现...



 '  System.Data.dll 
附加信息:ExecuteNonQuery:Connection属性没有发生 已经初始化。

解决方案

你第二次在cmd上没有连接:

 cmd = new SqlCommand(INSERT INTO [info](kod,name,family,tel,shahr,mantaghe)VALUES('+ textBox1.Text +','+ textBox2.Text +',' + textBox3.Text +','+ textBox4.Text +','+ textBox5.Text +','+ textBox6.Text +')); 





应该是:



 cmd =新的SqlCommand(INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe)VALUES('+ textBox1.Text +', + textBox2.Text +','+ textBox3.Text +','+ textBox4.Text +','+ textBox5.Text +','+ textBox6.Text +') ,con); 





注意加入了con ...



但是无论如何,这都行不通,因为你更改了cmd,我认为while循环会抛出异常。



 SqlCommand cmdInsert = new SqlCommand(INSERT INTO [info](kod,name,family,tel,shahr,mantaghe)VALUES('+ textBox1.Text +','+ textBox2.Text +','+ textBox3。 Text +','+ textBox4.Text +','+ textBox5.Text +','+ textBox6.Text +'),con); 
cmdInsert.ExecuteNonQuery();





再次,不建议这样做,使用SqlParameter传递来自所有的信息文本框。


SqlConnection con = new SqlConnection();
            con.ConnectionString = "Server=(Local); Database=final; Integrated Security=True";
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM [zarfiat] WHERE shahr=@shahr AND mantaghe=@mantaghe",con);
            cmd.Parameters.AddWithValue("shahr",textBox5.Text);
            cmd.Parameters.AddWithValue("mantaghe", textBox6.Text);
            SqlDataReader dr = null;
            dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                zarfiat=Convert.ToInt32(dr["zarfiat"]);
                if (zarfiat > 0)
                {
                    if (textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox4.Text != "" & textBox5.Text != "" & textBox6.Text != "")
                    {
                        cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')");
                        cmd.ExecuteNonQuery();
                    }
                    else label8.Text = "پر کردن تمامی اطلاعات الزامی می باشد";
                }
                else label8.Text = "ظرفیت خالی در  مکان مورد نظر موجود نمی باشد";
            }
            zarfiat--;
            cmd = new SqlCommand("UPDATE [final] SET zarfiat='"+zarfiat+"'");
            cmd.ExecuteNonQuery();
            label8.Text = "مشترک مورد نظر با موفقیت ثبت شد";
            con.Close()





This error appear...

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: ExecuteNonQuery: Connection property has not been initialized.

解决方案

You have NO Connection on cmd the second time around:

cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')");



Should be:

cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')",con);



notice that con was added...

But anyhow, this will NOT Work, as you change the cmd, the while loop will throw an exception, I think.

SqlCommand cmdInsert = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')",con);
cmdInsert.ExecuteNonQuery();



And Again, this is not recommended, use SqlParameter to pass the informations from all the textboxes.


这篇关于tis连接有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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