如何给SqlClient.SqlParameter类型赋值?

feng1959 2005-12-22 05:17:04
我现在想往SqlDataAdapter.SelectCommand.Parameters中赋多个值,代码如下:

Dim adapter As New SqlClient.SqlDataAdapter
Dim aa() As SqlClient.SqlParameter
Public userid As String
Public password As String
Userid= "DotNet"
Password="DotNet"

aa(0).Value = userid 此处总报错, 'System.NullReferenceException' のハンドルされていない
aa(1).Value = password

……

For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next
该如何给SqlClient.SqlParameter类型赋值?另外如何给object类型赋值?问题可能比较菜,还请大虾们帮忙啊.
...全文
213 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feng1959 2005-12-27
  • 打赏
  • 举报
回复
晕,不是这样吧.
不过还是先谢了.
feng1959 2005-12-23
  • 打赏
  • 举报
回复
谢谢楼上的,但还是通不过.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adapter As New SqlClient.SqlDataAdapter
Dim conn As New SqlClient.SqlConnection(connString)
Dim account As New DataSet
Dim aa(1) As SqlParameter
aa(0) = New SqlParameter("@UserId", SqlDbType.NChar)
aa(1) = New SqlParameter("@Password", SqlDbType.NChar)

aa(0).Value = "DotNet"
aa(1).Value = "DotNet"

conn.Open()
adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT

For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next

adapter.Fill(account)
adapter.Dispose()
conn.Close()

DataGrid1.DataSource = account.Tables(0)
End Sub

在adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT这一句有错误.
hamadou 2005-12-23
  • 打赏
  • 举报
回复
问题从这里开始出现:

conn.Open()
‘下面两句存在矛盾,本身不是一个存储过程,为何要把类型设置为存储过程?
adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT
adapter.SelectCommand.CommandType = CommandType.StoredProcedure

For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next
‘executereader本身是独占连接的,你这么做fill是用来做什么的呢?将executereader去掉。
adapter.SelectCommand.ExecuteReader()
adapter.Fill(account)
adapter.Dispose()
conn.Close()

DataGrid1.DataSource = account.Tables(0)
kjq_vb 2005-12-23
  • 打赏
  • 举报
回复

adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT
For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next
改为
For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next
adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT
feng1959 2005-12-22
  • 打赏
  • 举报
回复
看下面这个能清楚一点.

Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
'windows自带的代码
Public Const connString As String = "integrated security=sspi;initial catalog=pubs;data source=(local)"
Private Const SQL_SELECT_ACCOUNT As String = "SELECT Account.Email, Account.FirstName, Account.LastName, Account.Addr1, Account.Addr2, Account.City, Account.State, Account.Zip, Account.Country, Account.Phone, Profile.LangPref, Profile.FavCategory, Profile.MyListOpt, Profile.BannerOpt FROM Account INNER JOIN Profile ON Account.UserId = Profile.UserId INNER JOIN SignOn ON Account.UserId = SignOn.UserName WHERE SignOn.UserName = @UserId AND SignOn.Password = @Password"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adapter As New SqlClient.SqlDataAdapter
Dim conn As New SqlClient.SqlConnection(connString)
Dim account As New DataSet
Dim aa(1) As SqlParameter
aa(0) = New SqlParameter("@UserId", SqlDbType.NChar)
aa(1) = New SqlParameter("@Password", SqlDbType.NChar)

aa(0).Value = "DotNet" 'userid
aa(1).Value = "DotNet"

conn.Open()
adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT
adapter.SelectCommand.CommandType = CommandType.StoredProcedure

For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next

adapter.SelectCommand.ExecuteReader()
adapter.Fill(account)
adapter.Dispose()
conn.Close()

DataGrid1.DataSource = account.Tables(0)
End Sub
End Class
feng1959 2005-12-22
  • 打赏
  • 举报
回复
上面的问题我不想了,可能本身想法就是错的.麻烦各位帮我看看下面的程序错在什么地方?初学,程序漏洞百出,请各位帮忙给改改.谢了
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows フォーム デザイナで生成されたコード "

Public Sub New()
MyBase.New()

' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent()

' InitializeComponent() 呼び出しの後に初期化を追加します。

End Sub

' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer

' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更してください。
' コード エディタを使って変更しないでください。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 216)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 32)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(272, 168)
Me.DataGrid1.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region
Public Const connString As String = "integrated security=sspi;initial catalog=pubs;data source=(local)"
Private Const SQL_SELECT_ACCOUNT As String = "SELECT Account.Email, Account.FirstName, Account.LastName, Account.Addr1, Account.Addr2, Account.City, Account.State, Account.Zip, Account.Country, Account.Phone, Profile.LangPref, Profile.FavCategory, Profile.MyListOpt, Profile.BannerOpt FROM Account INNER JOIN Profile ON Account.UserId = Profile.UserId INNER JOIN SignOn ON Account.UserId = SignOn.UserName WHERE SignOn.UserName = @UserId AND SignOn.Password = @Password"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adapter As New SqlClient.SqlDataAdapter
Dim conn As New SqlClient.SqlConnection(connString)
Dim account As New DataSet
Dim aa(1) As SqlParameter
aa(0) = New SqlParameter("@UserId", SqlDbType.NChar)
aa(1) = New SqlParameter("@Password", SqlDbType.NChar)

aa(0).Value = "DotNet" 'userid
aa(1).Value = "DotNet"

conn.Open()
adapter.SelectCommand.CommandText = SQL_SELECT_ACCOUNT
adapter.SelectCommand.CommandType = CommandType.StoredProcedure

For Each cmdParms As SqlClient.SqlParameter In aa
adapter.SelectCommand.Parameters.Add(cmdParms)
Next

adapter.SelectCommand.ExecuteReader()
adapter.Fill(account)
adapter.Dispose()
conn.Close()

DataGrid1.DataSource = account.Tables(0)
End Sub
End Class
feng1959 2005-12-22
  • 打赏
  • 举报
回复
不要c#的代码,c#的和我要的有出入.
还有,我要添加的参数未知,Dim aa() As SqlClient.SqlParameter
在new SqlParameter时,声明的个数不清楚,不知该怎么解决?

LFreeX 2005-12-22
  • 打赏
  • 举报
回复
C#的代码?
SqlParameter[] parameters ={
new SqlParameter("@aaa", SqlDbType.Int),
new SqlParameter("@languageCodesdf", SqlDbType.VarChar)
};

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧