点击查询就执行一个SQL语句,为啥有错?

wincoman 2003-04-28 02:01:25
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChaxun.Click
' Set the SelectCommand properties...
myDataAdapter.SelectCommand = New SqlCommand()
myDataAdapter.SelectCommand.Connection = myConnection
myDataAdapter.SelectCommand.CommandText = _
"SELECT au_lname, au_fname, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_lname, au_fname"
myDataAdapter.SelectCommand.CommandType = CommandType.Text

' Open the database connection...
myConnection.Open()
' Now execute the command...
myDataAdapter.SelectCommand.ExecuteNonQuery()

' Fill the DataSet object with data...
myDataAdapter.Fill(myDataSet, "authors")

' Close the database connection...
myConnection.Close()

' Set the DataGrid properties to bind it to our data...
grdAuthorTitles.DataSource = myDataSet
grdAuthorTitles.DataMember = "authors"

' Declare objects for the DataGrid...
Dim objDataGridTableStyle As New DataGridTableStyle()
Dim objTextCol As New DataGridTextBoxColumn()

' Set the AlternatingBackColor property...
objDataGridTableStyle.AlternatingBackColor = Color.WhiteSmoke

' Set the MappingName for the DataGridTableStyle...
objDataGridTableStyle.MappingName = "authors"

' Set the MappingName for the first column...
objTextCol.MappingName = "au_lname"
' Set the new HeaderText...
objTextCol.HeaderText = "Last Name"
' Add the column to the DataGridTableStyle...
objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

' Get a new reference to the DataGridTextBoxColumn...
objTextCol = New DataGridTextBoxColumn()
' Set the MappingName for the second column...
objTextCol.MappingName = "au_fname"
' Set the new HeaderText...
objTextCol.HeaderText = "First Name"
' Add the column to the DataGridTableStyle...
objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

' Get a new reference to the DataGridTextBoxColumn...
objTextCol = New DataGridTextBoxColumn()
' Set the MappingName for the third column...
objTextCol.MappingName = "title"
' Set the new HeaderText...
objTextCol.HeaderText = "Book Title"
' Set the Width of the column...
objTextCol.Width = 304
' Add the column to the DataGridTableStyle...
objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

' Get a new reference to the DataGridTextBoxColumn...
objTextCol = New DataGridTextBoxColumn()
' Set the MappingName for the fourth column...
objTextCol.MappingName = "price"
' Set the new HeaderText...
objTextCol.HeaderText = "Retail Price"
' Set the Alignment within the column...
objTextCol.Alignment = HorizontalAlignment.Right
' Add the column to the DataGridTableStyle...
objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

' Add the DataGridTableStyle to the DataGrid...
grdAuthorTitles.TableStyles.Add(objDataGridTableStyle)

End Sub
...全文
48 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wincoman 2003-04-28
  • 打赏
  • 举报
回复
' Import Data and OleDb namespaces...
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
' Declare objects...
Dim myConnection As SqlConnection = New SqlConnection("server= (wangf);database=pubs;uid=sa;pwd=963")
Dim myDataAdapter As New SqlDataAdapter()
Dim myDataSet As DataSet = New DataSet()
holydiablo 2003-04-28
  • 打赏
  • 举报
回复
是啊

看这些代码和在一堆棉絮里找针是一样的
hilanderst 2003-04-28
  • 打赏
  • 举报
回复
先解决myConnection的问题,然后再说sql执行的问题;
先看看myConnection的设置。
heroyujun 2003-04-28
  • 打赏
  • 举报
回复
应该是
"SELECT authors.au_lname, authors.au_fname, authors.title,authors.price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_lname, au_fname"
wincoman 2003-04-28
  • 打赏
  • 举报
回复
在myConnection.Open()处,报"未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中
其他信息:系统错误。

zwztu 2003-04-28
  • 打赏
  • 举报
回复
请详细说明错误信息!
timmy3310 2003-04-28
  • 打赏
  • 举报
回复
出了什么错?
错误信息是什么

16,554

社区成员

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

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