需要帮助进行样本数据输入 [英] need help for sample data entry
本文介绍了需要帮助进行样本数据输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每个人都美好的一天
请在编写这个简单易用的程序时需要帮助
它只有三个文本框和一个按钮即可保存它们
这是我的代码
Imports System.Data Imports System.Data.SqlClient Public Class Form1 Dim cmd As SqlCommand Dim con As SqlConnection Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ConID As String Dim ConAddress As String Dim ConPhone As String ConID = Me.TextBox1.Text ConAddress = Me.TextBox2.Text ConPhone = Me.TextBox3.Text Try con.ConnectionString = "Data Source=DBS;Initial Catalog=Recruitment ;User Id=myuserid;Password=Mypassword" con.Open() cmd.Connection = con cmd.CommandText = "INSERT INTO Contacts(ConID, ConAddress,ConPhone ) VALUES(''" & ConID & "'' , ''" & ConAddress & "'' , ''" & ConPhone & "'') " Catch ex As Exception MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records") Finally con.Close() End Try End Sub End Class
解决方案
如果conID为数字,则无需将其用引号引起来.
" & ConID& " & ConAddress& ','"& ConPhone&
建议:您应该将命令与参数或过程一起使用.
不起作用:(
它一直告诉我这个错误
在表上插入时出错...对象引用未设置为对象的实例"
这是什么意思?
首先,在您的语句中使用参数,不要将文字连接到SQL文本.有关更多信息,请参见: SqlParameter [ Dim con As 新 SqlConnection Dim cmd As 新建 SqlCommand
另一件事是,您设置了sql语句,但是根据您的代码,您根本不会执行它.使用 ExecuteNonQuery [解决方案If conID is numeric, you need not enclose it in quotes.
"INSERT INTO Contacts(ConID, ConAddress,ConPhone ) VALUES(" & ConID & " , '" & ConAddress & "' , '" & ConPhone & "') "
Suggestion: You should use command with parameters or procedures.
not working :(
it keeps telling me this error
" Error while inserting on table... Object Reference not set to an instance of an object "
what is that mean ?
First of all, use parameters in your statement, don''t concatenate literals to the SQL text. More info, see: SqlParameter [^]. When you use parameters, you don''t have to worry for example about data type conversions etc.
The problem looks like that you define the connection and the command variables but you don''t create instances of those classes at all. So try changing to:
Dim con As New SqlConnection Dim cmd As New SqlCommand
Another thing is that you set the sql statement but based on your code you don''t execute it at all. Use ExecuteNonQuery[^] to actually execute the statement
这篇关于需要帮助进行样本数据输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文