简体   繁体   English

如何在Windows应用程序中通过VB.Net将数据插入SQL Server数据库

[英]How to insert data into SQL Server database by VB.Net in windows application

How to insert data into SQL Server database by VB.Net in Windows application. 如何在Windows应用程序中通过VB.Net将数据插入SQL Server数据库。 I am using the Windows form application. 我正在使用Windows窗体应用程序。
At run time it gives exception that: 在运行时,它给出以下异常:

con.open() the connection is not established. con.open()未建立连接。

code: 码:

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()
    ...
}

Where tbl1 is the table in the database. 其中tbl1是数据库中的表。

There are typos in your connection string. 您的连接字符串中有错别字。

  • Replace " Security " with " Integrated Security ". 将“ Security ”替换为“ Integrated Security ”。
  • Also, there should be a space in " Initial Catalog ". 另外,“ Initial Catalog ”中应该有一个空格。

you forgot using of '& &' for this line 您忘记为此行使用“&&”

Dim cmd as SqlCommand=new SqlCommand("insert into tbl1 values('"& textbox1.Text &"','"& textbox2.Text &"')",cn)

or use this link 或使用此链接

Try this (a standard connection): 尝试以下操作(标准连接):

Password=system;Persist Security Info=True;User ID=---;Initial Catalog=shri;Data Source=.\sqlexpress

If you want your password saved in connection string for future uses of connection string of connection object set Persist Security Info=True; 如果希望将密码保存在连接字符串中,以供将来使用连接对象集的连接字符串设置,则Persist Security Info=True; ; ; other ways set it Persist Security Info=False; 其他方式将其设置为Persist Security Info=False;

Actualy when you set Persist Security Info=False; 实际上,当您将Persist Security Info=False;设置Persist Security Info=False; ; ; The password of your connection string property of your connection object will hide. 连接对象的连接字符串属性的密码将隐藏。

Or If you use a trusted connection use this: 或者,如果您使用受信任的连接,请使用以下命令:

Integrated Security=SSPI;Initial Catalog=shri;Data Source=.\sqlexpress

Then; 然后; Change your sqlcommand to run a stored procedure for inserting is recommended. 建议更改您的sqlcommand以运行存储过程以进行插入。 But the better use of insert into is this: 但是insert into的更好用法是:

insert into <table Name> (<List of Fileds>) Values (<List of Values>)

And be careful about type of values for adding (') in your command string. 并且要注意在命令字符串中添加(')的值的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过TableAdapter vb.net将日期插入SQL Server数据库 - Insert date into SQL Server database via TableAdapter vb.net 如何授予SQL Server 2008数据库从C#Windows应用程序中的客户端系统插入数据的权限? - How to give the permissions to sql server 2008 database to insert the data from client system in c# windows application? 数据没有从数据表vb.net中按顺序插入SQL Server数据库中 - Data is not getting inserted into SQL Server database in orderly manner from datatable vb.net 如何将BLOB从vb.net Winforms应用程序流式传输到SQL Server并报告进度 - How can I stream a BLOB from vb.net winforms application to SQL Server and report progress 如何在 vb.net windows 应用程序中打印 html 页 - How to Print html page in vb.net windows application 如何在VB.Net中使用Windows窗体应用程序实现计划程序 - How to implement scheduler with Windows Form Application in VB.Net 如何减少来自.NET Windows应用程序的数据库(SQL Server)调用 - How to reduce database (SQL Server) calls from .NET windows application 有什么方法可以将Power Bi报表和仪表板与SQL Server 2008数据库一起嵌入vb.net或C#桌面应用程序中? - Is there any way to embed power bi reports and dashboards in vb.net or C# desktop application with sql server 2008 database? VB.NET SQL Server查询到LINQ - VB.NET SQL Server query to LINQ 尝试使用asp.net Web应用程序将数据插入SQL Server数据库,出现错误 - Trying to insert data into SQL Server database using asp.net web application, getting an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM