如何在vb.net中的microsoft sql server 2008数据库中从文本框中检索或插入值或文本 [英] how to retrive or insert values or text from textbox in microsoft sql server 2008 database in vb.net

查看:92
本文介绍了如何在vb.net中的microsoft sql server 2008数据库中从文本框中检索或插入值或文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Plz给我答复尽快从任何一个人因为我不能完成我的项目没有这个方法我必须在tommrow上提交我的应用程序所以plz给我答案

Plz Give me answer As soon as possible from any one because i can't do complete my project without this method i have to submit my application on tommrow so plz give me answer

推荐答案

以下是将记录INSERT到SQL服务器的分步过程:

1.创建VB.NET项目。

2.包括以下命名空间。

Imports System.Data

Imports System.Data.SqlClient



System.Data namespace提供对表示ADO.NET体系结构的类的访问,而System.Data.SqlClient命名空间是SQL Server的.NET Framework数据提供程序。



3.声明并实例化你的SQLConnection对象和Command对象,如下所示

Dim con As New SqlConnection

Dim cmd As New SqlCommand



4.将SQL连接字符串传递给SqlConnection对象的ConnectionString属性。

con.ConnectionSt ring =Data Source = atisource; Initial Catalog = BillingSys; Persist Security Info = True; User ID = sa; Password = 12345678



5.调用Open方法连接到SQL Server。

con.Open()



6.设置命令对象的连接。

cmd.Connection = con



7.将INSERT SQL语句传递给命令对象commandtext,如下所示

cmd.CommandText =INSERT INTO table(field1,[field2,...])VALUES(value1,[value2,...])



8.使用ExecuteNonQuery ()方法运行没有返回值的INSERT SQL。

cmd.ExecuteNonQuery()



如果您期望返回值,请使用ExecuteScalar ()代替。 ExecuteScalar的一个很好的例子是在插入记录后获取Identity列

(ExecuteScalar返回结果中第一行的第一列)。





将记录插入SQL数据库的完整示例源代码:



Dim con As New SqlConnection

Dim cmd As New SqlCommand

尝试

con.ConnectionString =Data Source = atisource; Initial Catalog = BillingSys; Persist Security Info = True; User ID = sa;密码= 12345678

con.Open()

cmd.Connection = con

cmd.CommandText =INSERT INTO table([field1] ,[field2])VALUES([Value1],[Value2])

cmd.ExecuteNonQuery()



Catch ex As Exception

MessageBox.Show(在表格上插入记录时出错...&ex.Message,插入记录)

最后

con .Close()

结束尝试

希望这能回答你的问题。如果您有任何疑虑,请随时告诉我。



谢谢

如果有帮助,请记得将回复标记为答案如果他们没有提供任何帮助,请取消标记。

欢迎使用一体化代码框架!如果您有任何反馈意见,请告诉我们。
Here is the step-by-step procedure to INSERT record to SQL server:
1. Create your VB.NET project.
2. Include the following namespaces.
Imports System.Data
Imports System.Data.SqlClient

The System.Data namespace provides access to classes that represent the ADO.NET architecture while the System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server.

3. Declare and instantiate your SQLConnection object and Command object as shown below
Dim con As New SqlConnection
Dim cmd As New SqlCommand

4. Pass the SQL connection string to ConnectionString property of your SqlConnection object.
con.ConnectionString = "Data Source=atisource;Initial Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678"

5. Invoke the Open Method to connect to SQL Server.
con.Open()

6. Set the connection of the command object.
cmd.Connection = con

7. Pass the INSERT SQL statement to the command object commandtext as shown below
cmd.CommandText = "INSERT INTO table (field1, [field2, ... ]) VALUES (value1, [value2, ...])"

8. Use the ExecuteNonQuery() method to run INSERT SQL that has no return value.
cmd.ExecuteNonQuery()

If you expect a return value use ExecuteScalar() instead. A good example of ExecuteScalar is to get the Identity column after inserting record
(ExecuteScalar return the first column of the first row from the result).


The full sample source code of inserting record to SQL Database:

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=atisource;Initial Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO table([field1], [field2]) VALUES([Value1], [Value2])"
cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
Hope this answers your question. If you have any concern, please feel free to let me know.

Thanks
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


这篇关于如何在vb.net中的microsoft sql server 2008数据库中从文本框中检索或插入值或文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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