如何连接数据库进行插入,重新进行 [英] how to connect databse for inserting,retriving
本文介绍了如何连接数据库进行插入,重新进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class AccOpeningPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsave.Click
Dim connetionString As String
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim sql As String
connetionString = "Data Source=abc-PC;Initial Catalog=aaadb;User ID=sa;Password=sa123"
cnn = New SqlConnection(connetionString)
cnn.Open()
MsgBox("Connection Open ! ")
sql = "insert into CUSTHEAD(CUSTID, BRCODE, SALUTEFLG, GENDER, CNAME, DOB, AGE, FORHNAME, ADD1, ACCTYPE, ACCNO) VALUES (@CUSTID,@BRCODE, @SALUTEFLG, @GENDER, @CNAME, @DOB, @AGE, @FORHNAME,@ ADD1, @ACCTYPE, @ACCNO)"
cmd = New SqlCommand(sql, cnn)
With cmd.Parameters
.AddWithValue("CUSTID", txtcustid.Text)
.AddWithValue("BRCODE", txtbrcode.Text)
.AddWithValue("SALUTEFLG", txtsalcode.Text)
.AddWithValue("GENDER", optmfj.SelectedItem.ToString())
.AddWithValue("CNAME", txtcname.Text)
.AddWithValue("DOB", txtdob.Text)
.AddWithValue("AGE", txtage.Text)
.AddWithValue("FORHNAME", txtforhname.Text)
.AddWithValue("ADD1", txtadd1.Text)
.AddWithValue("ACCTYPE", txtacctype.Text)
.AddWithValue("ACCNO", txtaccno.Text)
cmd.ExecuteNonQuery()
End With
cnn.Close()
End Sub
End Class
我得错误为
必须声明标量变量@。
描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的起源的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:必须声明标量变量
I get Error As
Must declare the scalar variable "@".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable
推荐答案
这个:
This:
cmd.Parameters.AddWithValue("CUSTID", txtcustid.Text)
应该是这样的
Should be like this
cmd.Parameters.AddWithValue("@CUSTID", txtcustid.Text)
这篇关于如何连接数据库进行插入,重新进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文