![](/img/trans.png)
[英]Insert date into SQL Server database via TableAdapter vb.net
[英]How to insert data into SQL Server database by VB.Net in windows application
如何在Windows應用程序中通過VB.Net將數據插入SQL Server數據庫。 我正在使用Windows窗體應用程序。
在運行時,它給出以下異常:
con.open()未建立連接。
碼:
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
是數據庫中的表。
您的連接字符串中有錯別字。
Security
”替換為“ Integrated Security
”。 Initial Catalog
”中應該有一個空格。 您忘記為此行使用“&&”
Dim cmd as SqlCommand=new SqlCommand("insert into tbl1 values('"& textbox1.Text &"','"& textbox2.Text &"')",cn)
或使用此鏈接
嘗試以下操作(標准連接):
Password=system;Persist Security Info=True;User ID=---;Initial Catalog=shri;Data Source=.\sqlexpress
如果希望將密碼保存在連接字符串中,以供將來使用連接對象集的連接字符串設置,則Persist Security Info=True;
; 其他方式將其設置為Persist Security Info=False;
實際上,當您將Persist Security Info=False;
設置Persist Security Info=False;
; 連接對象的連接字符串屬性的密碼將隱藏。
或者,如果您使用受信任的連接,請使用以下命令:
Integrated Security=SSPI;Initial Catalog=shri;Data Source=.\sqlexpress
然后; 建議更改您的sqlcommand
以運行存儲過程以進行插入。 但是insert into
的更好用法是:
insert into <table Name> (<List of Fileds>) Values (<List of Values>)
並且要注意在命令字符串中添加(')的值的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.