哪位能给我个例子?

uvvvw 2003-04-15 01:42:43
用vb.net操作sql server数据库,请给出完整的代码,包括数据库的连接,查询,加入,删除.

谢谢,分不是问题.
...全文
41 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
smiletosky 2003-04-15
  • 打赏
  • 举报
回复
太多拉,网上多的是
dragontt 2003-04-15
  • 打赏
  • 举报
回复
参考
http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=%2fquickstart%2faspplus%2fsamples%2fwebforms%2fDataBind%2fDataBind7.src

这种例子
网上很多
参考
http://chs.gotdotnet.com/quickstart/default.aspx
uvvvw 2003-04-15
  • 打赏
  • 举报
回复

to:dragontt(龙人)


你有没有直接写在<%%>中间的代码,就像以前的asp操作数据那样的代码
dragontt 2003-04-15
  • 打赏
  • 举报
回复
Imports System.Data
Imports System.Data.SqlClient
Public Class NorthWind
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写处置以清理组件列表。
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 DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents btnQuery As System.Windows.Forms.Button
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents RegionId As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents tbDesc As System.Windows.Forms.TextBox
Friend WithEvents tbId As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnQuery = New System.Windows.Forms.Button()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.btnAdd = New System.Windows.Forms.Button()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.RegionId = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.tbDesc = New System.Windows.Forms.TextBox()
Me.tbId = New System.Windows.Forms.TextBox()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'btnQuery
'
Me.btnQuery.Location = New System.Drawing.Point(8, 16)
Me.btnQuery.Name = "btnQuery"
Me.btnQuery.Size = New System.Drawing.Size(56, 23)
Me.btnQuery.TabIndex = 0
Me.btnQuery.Text = "查询"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(80, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(216, 96)
Me.DataGrid1.TabIndex = 1
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(8, 120)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(56, 23)
Me.btnAdd.TabIndex = 2
Me.btnAdd.Text = "新增"
'
'Panel1
'
Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.tbId, Me.RegionId, Me.Label1, Me.tbDesc})
Me.Panel1.Location = New System.Drawing.Point(88, 120)
Me.Panel1.Name = "Panel1"
Me.Panel1.TabIndex = 3
'
'RegionId
'
Me.RegionId.Location = New System.Drawing.Point(8, 8)
Me.RegionId.Name = "RegionId"
Me.RegionId.Size = New System.Drawing.Size(56, 16)
Me.RegionId.TabIndex = 0
Me.RegionId.Text = "RegionId"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "RegionDesc"
'
'tbDesc
'
Me.tbDesc.Location = New System.Drawing.Point(32, 64)
Me.tbDesc.Name = "tbDesc"
Me.tbDesc.Size = New System.Drawing.Size(136, 20)
Me.tbDesc.TabIndex = 1
Me.tbDesc.Text = ""
'
'tbId
'
Me.tbId.Location = New System.Drawing.Point(80, 8)
Me.tbId.Name = "tbId"
Me.tbId.TabIndex = 1
Me.tbId.Text = ""
'
'NorthWind
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(304, 358)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.btnAdd, Me.DataGrid1, Me.btnQuery})
Me.Name = "NorthWind"
Me.Text = "NorthWind"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

'Dim constr As String = "initial catalog=northwind;data source=localhost;Integrated Security=SSPI"
Dim constr As String = "initial catalog=northwind;data source=localhost;user id=sa;password=michal;"

Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
Dim table As New DataTable()
Dim ds As SqlDataAdapter
Dim sql As String = "select * from region"
ds = New SqlDataAdapter(sql, constr)
ds.Fill(table)
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = table
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If tbId.Text = String.Empty Then
MsgBox("RegionId不能为空")
Exit Sub
End If
Try
Integer.Parse(tbId.Text)
Catch
MsgBox("RegionId必须是整形")
Exit Sub
End Try

Dim command As SqlCommand
Dim sql As String = "insert into region values(@regionId,@regionDesc)"
command = New SqlCommand(sql, New SqlConnection(constr))
command.Parameters.Add(New SqlParameter("@regionId", tbId.Text))
command.Parameters.Add(New SqlParameter("@regionDesc", tbDesc.Text))
command.Connection.Open()
Try
command.ExecuteNonQuery()
MsgBox("新增成功")
Catch
MsgBox("新增失败,请检查资料是否重复")
End Try
command.Connection.Close()
End Sub
End Class


更详细的例子,请参考Dumuwish7.0 的vb.net版
uvvvw 2003-04-15
  • 打赏
  • 举报
回复
补充一下.是用dataset来做

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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