如何在Windows应用程序中通过VB.Net将数据插入SQL Server数据库 . 我正在使用Windows窗体应用程序 .
在运行时,它给出了以下异常:
con.open()未 Build 连接 .
码:
imports system.data
imports system.data.sqlclient
on button_click(){
Dim cn as SqlConnection=new SqlConnection("Data Source=.\sqlexpress;InitialCatlog=shri;Security=True;User id=---;password=system")
cn.close()
cn.Open()
Dim cmd as SqlCommand=new SqlCommand("insert into tbl1 values('"&textbox1.Text&"','"textbox2.Text"')",cn)
cmd.executeNonQuery()
messageBox.Text="record Inserted"
cn.close()
...
}
其中 tbl1
是数据库中的表 .
3 回答
您的连接字符串中存在拼写错误 .
替换“
Security
" with "Integrated Security
” .此外,“
Initial Catalog
”中应该有一个空格 .试试这个(标准连接):
如果您希望将密码保存在连接字符串中以供将来使用连接对象的连接字符串设置
Persist Security Info=True;
;其他方式设置它Persist Security Info=False;
Actualy当你设置
Persist Security Info=False;
;连接对象的连接字符串属性的密码将隐藏 .或者如果您使用可信连接,请使用以下命令:
然后;建议更改
sqlcommand
以运行存储过程以进行插入 . 但更好地使用insert into
是这样的:并且要注意在命令字符串中添加(')的值的类型 .
你忘了在这条线上使用'&&'
或使用此link