关于连接问题 [英] Regarding connection issue

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

问题描述

Hai下面是我的代码



Hai below is my code

String conecion = "provider=Microsoft.ACE.OLEDB.12.0;" + @"data source=d:\DataXL.xlsx;" + "Extended Properties='Excel 12.0 Xml;HDR=YES'";
        OleDbConnection con = new OleDbConnection(conecion);
       
 string sql = "INSERT INTO [Sheet1$] ([Name], [EmailId], [Phone no], [Address], [Height], [Weight], [Program], [Plan], [AmountPaid], [DueAmount])  VALUES(@nam,@emlid,@phonno,@addrs,@higt,@wigt,@selprog,@selplan,@amp,@amtdue)";
 OleDbCommand cmdd = new OleDbCommand(sql);
cmdd.Parameters.AddWithValue("@nam", nametxt.Text);
            cmdd.Parameters.AddWithValue("@emlid", emailtxt.Text);
            cmdd.Parameters.AddWithValue("@phonno", phno.Text);
            cmdd.Parameters.AddWithValue("@addrs", addresstxt.Text);
            cmdd.Parameters.AddWithValue("@higt", heighttxt.Text);
            cmdd.Parameters.AddWithValue("@wigt", weighttxt.Text);
            cmdd.Parameters.AddWithValue("@selprog", programtxt.SelectedItem.Text);
            cmdd.Parameters.AddWithValue("@selplan", DropDownList2.SelectedItem.Text);
            cmdd.Parameters.AddWithValue("@amp", paidamountxt.Text);
            cmdd.Parameters.AddWithValue("@amtdue", amountdue.Text);
            con.Open();
            cmdd.ExecuteNonQuery();
con.Close();



我得到的错误如下:


Error i am getting is give below:

connection property not initialized





请建议我。



谢谢。



Kindly suggest me.

Thankyou.

推荐答案

([姓名],[EmailId],[电话号码],[地址],[身高],[重量],[节目] ],[计划],[AmountPaid],[DueAmount])VALUES(@ nam,@ emlid,@ phonno,@ addrs,@ higt,@ wigt,@ selprog,@ selplan,@ amp,@ amtdue);
OleDbCommand cmdd = new OleDbCommand(sql);
cmdd.Parameters.AddWithValue( @nam,nametxt.Text);
cmdd.Parameters.AddWithValue( @ emlid,emailtxt.Text);
cmdd.Parameters.AddWithValue( @ phonno,phno.Text);
cmdd.Parameters.AddWithValue( @ addrs,addresstxt.Text);
cmdd.Parameters.AddWithValue( @ higt,heighttxt.Text);
cmdd.Parameters.AddWithValue( @ wigt,weighttxt.Text);
cmdd.Parameters.AddWithValue( @ selprog,programtxt.SelectedItem.Text);
cmdd.Parameters.AddWithValue( @ selplan,DropDownList2.SelectedItem.Text);
cmdd.Parameters.AddWithValue( @ amp,paidamountxt.Text);
cmdd.Parameters.AddWithValue( @ amtdue,amountdue.Text);
con.Open();
cmdd.ExecuteNonQuery();
con.Close();
([Name], [EmailId], [Phone no], [Address], [Height], [Weight], [Program], [Plan], [AmountPaid], [DueAmount]) VALUES(@nam,@emlid,@phonno,@addrs,@higt,@wigt,@selprog,@selplan,@amp,@amtdue)"; OleDbCommand cmdd = new OleDbCommand(sql); cmdd.Parameters.AddWithValue("@nam", nametxt.Text); cmdd.Parameters.AddWithValue("@emlid", emailtxt.Text); cmdd.Parameters.AddWithValue("@phonno", phno.Text); cmdd.Parameters.AddWithValue("@addrs", addresstxt.Text); cmdd.Parameters.AddWithValue("@higt", heighttxt.Text); cmdd.Parameters.AddWithValue("@wigt", weighttxt.Text); cmdd.Parameters.AddWithValue("@selprog", programtxt.SelectedItem.Text); cmdd.Parameters.AddWithValue("@selplan", DropDownList2.SelectedItem.Text); cmdd.Parameters.AddWithValue("@amp", paidamountxt.Text); cmdd.Parameters.AddWithValue("@amtdue", amountdue.Text); con.Open(); cmdd.ExecuteNonQuery(); con.Close();



我得到的错误如下:


Error i am getting is give below:

connection property not initialized





请建议我。



谢谢你。



Kindly suggest me.

Thankyou.


因为你还没有初始化命令的连接。



而不是......

because you have not initialized the connection for the command.

Instead of this...
OleDbCommand cmdd = new OleDbCommand(sql);



这样做......


Do this...

OleDbCommand cmdd = new OleDbCommand(sql, con);


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

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