Private Sub SQLM()
Dim myconn As String = "server=(local);UID=sa;PWD=123456; database=tj"
SqlConnection1 = New SqlClient.SqlConnection(myconn)
'sqlCommand1 = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
SqlDataAdapter1 = New SqlClient.SqlDataAdapter("select 分类1,id from 通信录1", SqlConnection1)
SqlDataAdapter1.Fill(DSLIST11, "通信录1")
DataGridView1.DataSource = DSLIST11.Tables(0)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'OleDbDataAdapter1.SelectCommand = New OleDb.OleDbCommand("select top 5 * from 通信录1", OleDbConnection1)
SqlDataAdapter1.SelectCommand = New SqlClient.SqlCommand("select top 5 * from 通信录1", SqlConnection1)
DSLIST11.Clear()
SqlDataAdapter1.Fill(DSLIST11, "通信录1")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call SQLM()
End Sub
posted @
2006-11-19 18:06 MT'S BLOG 阅读(163) |
评论 (0) |
编辑 收藏
<%@ Page Language="VB" Debug="true" trace="true" %>
<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.data.SQLClient" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, E as eventargs)
Dim ds as DataSet
Dim MyConnection as SQLConnection
Dim MyAdpt as SQLDataAdapter
MyConnection =New SQLConnection("server='localhost';UID=sa;PWD=wenasen.com; database=credit_sql")
MyAdpt = New SQLDataAdapter("select * from admin",MyConnection)
ds= new DataSet()
MyAdpt.Fill(ds,"admin")
dgA.DataSource=ds.tables("admin").defaultView
dgA.DataBind()
end sub
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>显示SQL数据库</title>
</head>
<body>
<form id="form1" runat="server">
<asp:datagrid id="dgA" runat="server" AllowPaging="true" Width="100%" CellPadding="1" EditItemStyle-BackColor=""
HeaderStyle-BackColor="#00CCCC" border="1" AlternatingItemStyle-BackColor="#CCCCCC"
></asp:datagrid>
</form>
</body>
</html>
posted @
2006-11-19 17:49 MT'S BLOG 阅读(378) |
评论 (0) |
编辑 收藏
ADO.Net的Programmer Guide
創稿人 |
蔡爾榮 |
創稿日期 |
2002/11/15 |
最後修改人 |
蔡爾榮 |
最後修改日期 |
2003/02/03 |
版本:1.06.00.0007
本文以SQL Server .Net managed data provider為準,若使用OleDB連接Database,可以在Class name用OleDb取代Sql。
本文不會詳述ADO.Net的使用方式,僅提供一brief introduction,詳細說明請自行參閱ADO.Net的OnLine Help。
ADO.Net-System.Data
DataSet
類似SQL Server的Database,DataSet中可以含DataTable,DataRelation。
DataTable
類似SQL Server的Table,DataTable上可以有DataView,DataTable與DataTable間可以有DataRelation。
DataTable可以有PrimaryKey。
當DataTable.NewRow時,DataTable中Columns的Default就會跑至新增DataRow中了。
當DataTable.Rows.Add(row),ADO.Net會檢查該Row是否違反Constraint。
There are several events that are raised by the DataTable object when a change is occurring in a record:
The ColumnChanging and ColumnChanged events are raised during and after each change to an individual column. The ColumnChanging event is useful when you want to validate changes in specific columns. Information about the proposed change is passed as an argument with the event.
The RowChanging and RowChanged events are raised during and after any change in a row. The RowChanging event is more general, in that it simply indicates that a change is occurring somewhere in the row; you do not know which column has changed.
By default, each change to a column therefore raises four events: first the ColumnChanging and ColumnChanged events for the specific column being changed, and then the RowChanging and RowChanged event. If multiple changes are being made to the row, the events will be raised for each change.
Note The data row's BeginEdit method turns off the RowChanging and RowChanged events after each individual column change. In that case, the event is not raised until the EndEdit method has been called, when the RowChanging and RowChanged events are raised just once.
The event you choose depends on how granular you want the validation to be. If it is important that you catch an error immediately when a column is changed, build validation using the ColumnChanging event. Otherwise, use the RowChanging event, which might result in catching several errors at once. Additionally, if your data is structured in such a way that the value of one column is validated based on the contents of another column then you should perform your validation during the RowChanging event.
DataView
類似SQL Server的View,DataView依附於DataTable,本身不存資料,資料存在DataTable中。
DataRow
DataTable中的一筆資料。
有RowState決定目前Row是新增刪除或修改。
具有double data buffer的功能。
DataRowCollection類似ADO.Recordset
The DataRowVersion informs you what version of a DataRow exists. Versions change under the following circumstances:
- After calling the DataRow object's BeginEdit method, if you change the value, the Current and Proposed values become available.
- After calling the DataRow object's CancelEdit method, the Proposed value is deleted.
- After calling the DataRow object's EndEdit method, the Proposed value becomes the Current value.
- After calling the DataRow object's AcceptChanges method, the Original value becomes identical to the Current value.
- After calling the DataTable object's AcceptChanges method, the Original value becomes identical to the Current value.
- After calling the DataRow object's RejectChanges, the Proposed value is discarded, and the version becomes Current.
DataRowVersion
當DataRow.HasVersion(DataRowVersion.Proposed) == true時,DataRow["DataCol"]會與DataRow["DataCol",DataRowVersion.Proposed]相同。
當DataRow.HasVersion(DataRowVersion.Proposed) == false時,DataRow["DataCol"]會與DataRow["DataCol",DataRowVersion.Current]相同。
DataRowView
DataView中的一筆資料。
DataColumn
記載DataTable中的Column Schema。
有AutoIncrement Column類似SQL Server Identity的功能。
有計算欄位的功能。
當DataColumn被Assign到某Table後,該DataColumn的MaxLength便無法修改。
DataRelation
類似SQL Server的Foreign key constrain,描述著DataTable與DataTable之間的關係。
SQLClient-System.Data.SqlClient
SqlDataAdapter
ADO.Net與Database溝通的Class。可將SQL Server上的Table讀進DataTable,亦可將DataTable的異動寫回Database。
SqlConnection
類似ADO.Connection。
SqlParameter
類似ADO.Parameter。
SqlCommand
類似ADO.Command
在使用SqlCommand.ExecuteScalar(含IDbCommand.ExecuteScalar)時須注意,當SqlCommand.CommandText的語法錯誤時,會throw Exception,但是若僅僅是型別有錯,是不會throw Exception的,但是該SqlConnection會自動被Rollback,且ExecuteScalar return null。
例如:CaseMap.MapID在資料庫的型別為int
sqlConnection1.Open();
IDbCommand SQLCommand = new SqlCommand("select count(*) from CaseMap where MapID='rwerwe'",sqlConnection1);
SQLCommand.ExecuteScalar(); // 不會throw Exception
sqlConnection1.Close();
sqlConnection1.Open();
IDbCommand SQLCommand = new SqlCommand("select count(*) from CaseMap where1 MapID='1'",sqlConnection1);
SQLCommand.ExecuteScalar(); // 會throw Exception
sqlConnection1.Close();
TQuarkLib.Net的TQuark.Lib.Data.DBHelp.ExecuteScalarMustHaveValue提供一Partial Solution。當IDbCommand.ExecuteScalar return null時,會throw exception,但是依然無法恢復Transaction的狀態,因為該Connection已經被sp_reset_connection了。
SqlDataReader
可自SQL Server一筆一筆資料讀取的class。
SqlDataAdapter.Update與Transaction的關係
當呼叫SqlDataAdapter.Update時,若發生Exception,因為Update是Process by Row by row的,理論上已經做完的Row其RowState應該已經AcceptChange。但是若後來SQL Rollback會造成State不符。
Read and Write BLOB Data by Using ADO.NET
待研究
TQuarkLib.Net的支援
DataHelp
提供一些有關DataSet,DataTable,DataView,DataRow間處理的utility。
DBHelp
提供一些有關DataSet,DataTable,DataView,DataRow與IDbCommand的處理utility。
SqlHelp
提供一些有關DataSet,DataTable,DataView,DataRow與SqlClient的處理utility。
DataSetTransactionControl
類似NestedTransactionControl,但是NestedTransactionControl是支援Connection,而DataSetTransactionControl是支援DataSet。
DataSetTransactionMonitor
類似DBTransactionMonitor,但是DBTransactionMonitor是支援Connection,而DataSetTransactionMonitor是支援DataSet。
posted @
2006-11-19 17:45 MT'S BLOG 阅读(826) |
评论 (0) |
编辑 收藏
Module ModSql
Function GetIntByStr(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "='" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), 0, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetIntByInt(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As Integer) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "=" & ParaValue & ""
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), 0, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetStrByStr(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As String) As String
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "='" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As String = ""
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), "", sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return Trim(rowsAffected)
End Function
Function GetStrByInt(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As Integer) As String
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "=" & ParaValue & ""
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As String = ""
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), "", sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return Trim(rowsAffected)
End Function
Function GetBitByStr(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As String) As Boolean
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "='" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Boolean = False
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), False, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetBitByInt(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As Integer) As Boolean
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "=" & ParaValue & ""
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Boolean = False
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), False, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetDateByInt(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As Integer) As DateTime
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "=" & ParaValue & ""
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As DateTime
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), DateAdd(DateInterval.Year, -100, Date.Now), sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetDateByStr(ByVal FieldName As String, ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As String) As DateTime
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "select Top 1 " & FieldName & " from " & TableName & " where " & ParaName & "='" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As DateTime
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), DateAdd(DateInterval.Year, -100, Date.Now), sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function DelByInt(ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As Integer) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "DELETE FROM " & TableName & " WHERE " & ParaName & " =" & ParaValue
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function DelByStr(ByVal TableName As String, ByVal ParaName As String, ByVal ParaValue As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "DELETE FROM " & TableName & " WHERE " & ParaName & "='" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function DelBySQL(ByVal StrSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = StrSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateBitBySQL(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As Boolean, ByVal ParaSQL As String) As String
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = " & IIf(DataFieldValue = True, 1, 0) & " WHERE " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Try
sqlCommand.ExecuteNonQuery()
Return ""
Catch ex As Exception
Return ex.Message
Finally
sqlConnection.Close()
End Try
End Function
Function UpdateStrByInt(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As String, ByVal Para As String, ByVal ParaValue As Integer) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = '" & DataFieldValue & "' WHERE " & Para & " = " & ParaValue
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateStrByStr(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As String, ByVal Para As String, ByVal ParaValue As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = '" & DataFieldValue & "' WHERE " & Para & " = '" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateIntByInt(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As Integer, ByVal Para As String, ByVal ParaValue As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = " & DataFieldValue & " WHERE " & Para & " = " & ParaValue
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateIntByStr(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As Integer, ByVal Para As String, ByVal ParaValue As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = " & DataFieldValue & " WHERE " & Para & " = '" & ParaValue & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateIntBySQL(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As Integer, ByVal ParaSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = " & DataFieldValue & " WHERE " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateStrBySQL(ByVal TableName As String, ByVal DataFieldName As String, ByVal DataFieldValue As String, ByVal ParaSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & DataFieldName & " = '" & DataFieldValue & "' WHERE " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function UpdateBySQL(ByVal TableName As String, ByVal SetSQL As String, ByVal ParaSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "UPDATE " & TableName & " SET " & SetSQL & " WHERE " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetDataTableByFormat(ByVal vRecordNumber As Integer, ByVal ItemSQL As String, ByVal TableName As String, ByVal ParaSQL As String) As System.Data.DataTable
Dim RecordNumber As String
If vRecordNumber = 0 Then
RecordNumber = ""
Else
RecordNumber = "TOP " & vRecordNumber
End If
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT " & RecordNumber & " " & ItemSQL & " FROM " & TableName & " where " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
Try
dataAdapter.Fill(dataSet)
Return dataSet.Tables(0)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
End Function
Function GetDataTableBySQL(ByVal SuperSQL As String) As System.Data.DataTable
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(SuperSQL, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
Try
dataAdapter.Fill(dataSet)
Return dataSet.Tables(0)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
End Function
Function GetMaxField(ByVal TableName As String, ByVal FieldName As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT MAX(" & FieldName & ") AS MaxField FROM " & TableName
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), 0, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetMinField(ByVal TableName As String, ByVal FieldName As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT Min(" & FieldName & ") AS MaxField FROM " & TableName
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer = 0
sqlConnection.Open()
Try
rowsAffected = IIf(IsDBNull(sqlCommand.ExecuteScalar), 0, sqlCommand.ExecuteScalar)
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetFieldCount(ByVal TableName As String, ByVal ParaSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT COUNT(*) AS FieldCount FROM " & TableName & " where " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteScalar
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetFieldSumByInt(ByVal TableName As String, ByVal FieldName As String, ByVal ParaSQL As String) As Integer
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT isnull(sum(" & FieldName & "),0) AS CountNumber FROM " & TableName & " where " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Integer
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteScalar
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function GetFieldSumByDec(ByVal TableName As String, ByVal FieldName As String, ByVal ParaSQL As String) As Decimal
Dim sqlConnection As New SqlClient.SqlConnection(GetConn)
Dim queryString As String = "SELECT isnull(sum(" & FieldName & "),0) AS CountNumber FROM " & TableName & " where " & ParaSQL
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim rowsAffected As Decimal
sqlConnection.Open()
Try
rowsAffected = sqlCommand.ExecuteScalar
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
Finally
sqlConnection.Close()
End Try
Return rowsAffected
End Function
Function ChgFieldByLeftWord(ByVal TableName As String, ByVal FieldName As String, ByVal ParaWord As String, ByVal NewWord As String) As Integer
Try
Return UpdateBySQL(TableName, FieldName & "='" & NewWord & "'+right(" & FieldName & ",len(" & FieldName & ")-" & Len(ParaWord) & ")", "Len(" & FieldName & ")>=" & Len(ParaWord) & " and left(" & FieldName & "," & Len(ParaWord) & ")='" & ParaWord & "'")
Catch ex As Exception
Throw New NotSupportedException(ex.Message)
End Try
End Function
'数据库连接串
Private Function GetConn() As String
Return "server=localhost;database=pubs;uid=sa;pwd="
End Function
End Module
posted @
2006-11-19 10:46 MT'S BLOG 阅读(354) |
评论 (0) |
编辑 收藏
Imports System
Imports System.Data
Imports System.Drawing
Imports System.Data.OleDb
Module ConnToDB
Public StartPath As String = Application.StartupPath & "\LiberySystem"
Public Connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
Public ConnectStr As String = Connstr & "Data Source=" & StartPath
Public DataAdapter As OleDbDataAdapter
Public DataConnection As OleDbConnection
Public DataSet As DataSet
Public Function GetDataFromDB(ByVal sqlstr As String) As DataSet
Try
DataConnection = New OleDbConnection
DataConnection.ConnectionString = ConnectStr
DataAdapter = New OleDbDataAdapter(sqlstr, DataConnection)
DataSet = New DataSet
DataSet.Clear()
DataAdapter.Fill(DataSet)
DataConnection.Close()
Catch
MsgBox(Err.Description)
End Try
If DataSet.Tables(0).Rows.Count > 0 Then
Return DataSet
Else
Return Nothing
End If
End Function
Public Function UpdateDataBase(ByVal sqlstr As String) As Boolean
Dim sqlconn As OleDbConnection
Try
Dim cmdTable As OleDbCommand = New OleDbCommand(sqlstr, sqlconn)
cmdTable.CommandType = CommandType.Text
sqlconn.Open()
cmdTable.ExecuteNonQuery()
sqlconn.Close()
Catch
MessageBox.Show(Err.Description)
End Try
Return True
End Function
Public Class Dictionary
Public ISBN As String
Public FineBase As Double
Public FineMulti As Double
Public FineFactor As Double
End Class
Public Structure OnceLends
Public ReadStyle As String
Public LendBooks As Integer
End Structure
Public Structure BorrowType
Public Type As String
Public Days As Integer
End Structure
End Module
posted @
2006-11-19 10:40 MT'S BLOG 阅读(298) |
评论 (0) |
编辑 收藏
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DSLIST11.Clear()
OleDbDataAdapter1.Fill(DSLIST11, "通信录1")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.BindingContext(DSLIST11, "通信录1").Position -= 1
showp()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.BindingContext(DSLIST11, "通信录1").Position += 1
showp()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=d:\mydoc\Visual Studio 2005\Projects\PRJ_dbCONN\PRJ_dbCONN\test1.mdb;"
OleDbConnection1 = New OleDb.OleDbConnection(myconn) '¹
OleDbDataAdapter1 = New OleDbDataAdapter("select 分类1,id from 通信录1", OleDbConnection1) '2
OleDbDataAdapter1.Fill(DSLIST11, "通信录1") '3
DataGridView1.DataSource = DSLIST11.Tables(0) '4
'TextBox2.Text = Me.Name
End Sub
Private Sub showp()
Dim iCnt As Integer
Dim iPos As Integer
iCnt = Me.BindingContext(DSLIST11, "通信录1").Count
If iCnt = 0 Then
TextBox2.Text = "(No records)"
Else
iPos = Me.BindingContext(DSLIST11, "通信录1").Position + 1
TextBox2.Text = iPos.ToString & " of " & iCnt.ToString
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
OleDbDataAdapter1.SelectCommand = New OleDb.OleDbCommand("select top 5 * from 通信录1", OleDbConnection1)
DSLIST11.Clear()
OleDbDataAdapter1.Fill(DSLIST11, "通信录1")
End Sub
Private Sub OleDbConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbInfoMessageEventArgs) Handles OleDbConnection1.InfoMessage
End Sub
End Class
posted @
2006-11-12 21:16 MT'S BLOG 阅读(216) |
评论 (0) |
编辑 收藏
Public Class Form1
Inherits System.Windows.Forms.Form
'Dim myconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=D:\work\gdC\test.mdb;"
'Dim ADC As OleDb.OleDbConnection = New OleDb.OleDbConnection(myconn)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DslisT11.Clear()
OleDbDataAdapter1.Fill(DslisT11, "通信录") 'ERROR -->Fill: SelectCommand.Connection 属性尚未初始化
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.BindingContext(DslisT11, "通信录").Position -= 1
showp()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.BindingContext(DslisT11, "通信录").Position += 1
showp()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox2.Text = Me.Name
End Sub
Private Sub showp()
Dim iCnt As Integer
Dim iPos As Integer
iCnt = Me.BindingContext(DslisT11, "通信录").Count
If iCnt = 0 Then
TextBox2.Text = "(No records)"
Else
iPos = Me.BindingContext(DslisT11, "通信录").Position + 1
TextBox2.Text = iPos.ToString & " of " & iCnt.ToString
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
OleDbDataAdapter1.SelectCommand = New OleDb.OleDbCommand("select top 5 * from 通信录", OleDbConnection1)
DslisT11.Clear()
OleDbDataAdapter1.Fill(DslisT11, "通信录")
End Sub
End Class
HOW TO
refer to
http://doc.readmen.com/1/141059.shtml as the follow
Dim mySelectText As String = "SELECT CustomerID, CompanyName FROM CUSTOMERS"
Dim myConnString As String = "Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"
Dim custDA As OleDbDataAdapter = New OleDbDataAdapter(mySelectText, myConnString)
Solution: add the command string for OleDbDataAdapter1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox2.Text = Me.Name
OleDbDataAdapter1 = New OleDb.OleDbDataAdapter("select * from 通信录", OleDbConnection1)
End Sub
[FINISH]
posted @
2006-11-12 15:33 MT'S BLOG 阅读(3210) |
评论 (0) |
编辑 收藏