使用SQL Management Studio将数据插入我的数据库时执行ExecuteNonQuery [英] ExecuteNonQuery while inserting the data into my database using SQL Management Studio
本文介绍了使用SQL Management Studio将数据插入我的数据库时执行ExecuteNonQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
将数据插入数据库时遇到问题,我收到此消息
Hi
i have a problem while inserting the data into database i am getting this message
ExecuteNonQuery:Connection property has not been initialized
所以请让我指导解决这个问题?
代码
So please let me guide to solve this problem??
CODE
private void BtnCreate_Click(object sender, EventArgs e)
{
try
{
sqlCon.Open();
string a = (TBUserName.Text);
string d = (TBFirstName.Text);
string e1 = (TBLastName.Text);
string f = (TBMiddleName.Text);
string g = (TBEmail.Text);
string p = (TBPassword.Text);
//string rp= (TBRTPassword.text);
string country = (CBCountry.Text);
SqlCommand sc1 = new SqlCommand("INSERT INTO UserCreation_Table (user_id, Password, First_Name,Last_Name,Email,Country)"+ " Values ('" + TBUserName.Text + "', '" + TBPassword.Text + "', " + " '" + TBFirstName.Text + "', " + " '" + TBLastName.Text + "','t@domain.com','Pakistan') ");
sc1.Connection = sqlCon;
//MessageBox.Show("" + sc1);
//string s = Convert.ToString(sc1);
sc1.ExecuteNonQuery();
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
sqlCon.Close();
推荐答案
Google [
http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/d6a74623-89b0-4b9a-ad07-4b8e1a79015c/[^]
Google[^] the same.
您必须首先使用适当的连接字符串设置SqlConnection对象,然后在其上调用Open方法.否则,它不知道要访问哪个数据库!
You must first set up a SqlConnection object, with the appropriate connections string, and call the Open method on it. Otherwise it does not know which database to access!
using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Documents and Settings\user\My Documents\My Projects\SoftwareMatters\App_Data\ASPNETDB.MDF"";Integrated Security=True;User Instance=True"))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT UserName FROM aspnet_Users", con))
{
using (SqlDataReader r = cmd.ExecuteReader())
{
while (r.Read())
{
Response.Write((string) r[0] + "<br />");
}
}
}
}
hai,
没问题,很容易.可能是您没有使用连接打开和连接初始化.
hai,
no problem, it is easy., may be you didn''t use connection open and connection initialize.
SqlConnection con=new SqlConnection("server=.;database=dbname;integrated security=true;");<br />
con.open();<br />
SqlCommand cmd=new SqlCommand("...Here your query...", con);<br />
<br />
您需要初始化连接后的查询.
并进一步..
我希望这对您有用
小心...
your query after you need initialize your connection.
and further on..
i hope this is useful to you
take care...
这篇关于使用SQL Management Studio将数据插入我的数据库时执行ExecuteNonQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文