您的位置:首页 > 数据库

vb.net2005资料收集(数据库篇)

2007-10-06 22:13 281 查看
一、数据库链接

1、使用OleDbConnection、OleDbCommand、OleDbDataReader连接读取数据库


Dim MyConnection As New OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "Northwind.mdb")


Dim MyCommand As New OleDbCommand(Me.TextBox1.Text, MyConnection)


MyConnection.Open()


Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader()


Dim MyTable As New DataTable()


MyTable.Load(MyReader)


Me.DataGridView1.DataSource = MyTable


MyConnection.Close()

2、 使用DataSet、OleDbDataAdapter来填充数据


Me.DataSet1 = New DataSet()


Me.OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "Northwind.mdb"


Me.OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM 客户"


Me.OleDbDataAdapter1.SelectCommand.Connection = Me.OleDbConnection1


Me.OleDbDataAdapter1.Fill(Me.DataSet1, "客户")


Me.DataGrid1.DataSource = Me.DataSet1.Tables(0)



3、其他Connection连接


在MSDN中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式罗列一些,具体的每一项代表的意义可以参看MSDN.




ADO.net 中数据库连接方式(微软提供)




微软提供了以下四种数据库连接方式:


System.Data.OleDb.OleDbConnection


System.Data.SqlClient.SqlConnection


System.Data.Odbc.OdbcConnection


System.Data.OracleClient.OracleConnection


下面我们以范例的方式,来依次说明:




System.Data.SqlClient.SqlConnection


常用的一些连接字符串(C#代码):




SqlConnection conn


= new SqlConnection( "Server=(local);Integrated Security=SSPI;database=Pubs");




SqlConnection conn


= new SqlConnection("server=(local)/NetSDK;database=pubs;Integrated Security=SSPI");




SqlConnection conn = new SqlConnection(


"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");




SqlConnection conn = new SqlConnection(


" data source=(local);initial catalog=xr;integrated security=SSPI;


persist security info=False;workstation id=XURUI;packet size=4096; ");




SqlConnection myConn = new


System.Data.SqlClient.SqlConnection("Persist Security Info=False;Integrated


Security=SSPI;database=northwind;server=mySQLServer");




SqlConnection conn = new SqlConnection(


" uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900");




更多字符串连接说明请看MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.asp



System.Data.OleDb.OleDbConnection


常用的一些连接字符串(C#代码):




OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:MyWeb81GrocerToGo.mdb");




OleDbConnection conn = new OleDbConnection(


@"Provider=Microsoft.Jet.OLEDB.4.0;Password=;


User ID=Admin;Data Source=grocertogo.mdb;");




OleDbConnection conn = new OleDbConnection(


"Provider=MSDAORA; Data Source=ORACLE8i7;Persist Security Info=False;Integrated Security=yes");




OleDbConnection conn = new OleDbConnection(


"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:inLocalAccess40.mdb");




OleDbConnection conn = new OleDbConnection(


"Provider=SQLOLEDB;Data Source=MySQLServer;Integrated Security=SSPI");




更多字符串连接说明请看MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionStringTopic.asp?frame=true





System.Data.OracleClient.OracleConnection


常用的一些连接字符串(C#代码):




OracleConnection myConn = new System.Data.OracleClient.OracleConnection(


"Data Source=Oracle8i;Integrated Security=yes");








更多字符串连接说明请看MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOracleClientOracleConnectionClassConnectionStringTopic.asp?frame=true





System.Data.Odbc.OdbcConnection


常用的一些连接字符串(C#代码):






OdbcConnection conn = new OdbcConnection(


"Driver={SQL Server};Server=MyServer;Trusted_Connection=yes;Database=Northwind;");




OdbcConnection conn = new OdbcConnection(


"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;


Persist Security Info=False;Trusted_Connection=yes");




OdbcConnection conn = new OdbcConnection(


"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:in wind.mdb");




OdbcConnection conn = new OdbcConnection(


"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:inook1.xls");






OdbcConnection conn = new OdbcConnection(


"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:in");




OdbcConnection conn = new OdbcConnection("DSN=dsnname");




更多字符串连接说明请看MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOdbcOdbcConnectionClassConnectionStringTopic.asp?frame=true





其他厂商提供的数据库连接:




DB2Connection myConn = new IBM.Data.DB2.DB2Connection(


"DATABASE = SAMPLE;UID=; PWD=;");




DB2Connection myConn = new IBM.Data.DB2.DB2Connection("DATABASE = SAMPLE");






BdpConnection myConn = new Borland.Data.Provider.BdpConnection("assembly=Borl


and.Data.Mssql,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b;ve


ndorclient=sqloledb.dll;osauthentication=False;database=;usernam


e=;hostname=;password=;provider=MSSQL");




BdpConnection myConn = new Borland.Data.Provider.BdpConnection("assembly=Borl


and.Data.Db2,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b;ve


ndorclient=db2cli.dll;database=;username=;


password=;provider=DB2");






Connection Pooling






在SQL Server、OLE DB和.NET框架结构中的Data Provider中,都提供了隐式的连接池连接支持。你可以在ConnectionString中指定不同的参数值控制连接池的行为。比如下面的例子使OLE DB的连接池无效并自动地进行事务处理:


Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integrated Security=SSPI;


在SQL Server.NET Data Provider中提供了以下参数设置控制连接池的行为:Connection Lifttime、Connection Reset、Enlist、Max Pool Size、Min Pool Size和Pooling。




更多数据库连接信息,以及非ADO.net的连接字符串可以参看:

http://www.connectionstrings.com/








二、批量导入MSSQL


Imports System.Data.SqlClient


Imports System.Data.OleDb






Public Class Form1Class Form1


'执行SQL查询语句




Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Try


Dim MyConnection As New OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "Northwind.mdb")


Dim MyCommand As New OleDbCommand(Me.TextBox1.Text, MyConnection)


MyConnection.Open()


Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader()


Dim MyTable = New DataTable()


MyTable.Load(MyReader)


DataGridView1.DataSource = MyTable


MyConnection.Close()


Catch ex As Exception


MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Try


End Sub


'导入SQL Server数据库




Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Try


Dim MyString As String = "Data Source=(local);Integrated Security=SSPI; Database=Northwind"


Dim MyConnection As New SqlConnection(MyString)


MyConnection.Open()


Dim MyBulkCopy As New SqlBulkCopy(MyConnection)


MyBulkCopy.DestinationTableName = Me.TextBox2.Text


MyBulkCopy.WriteToServer(CType(DataGridView1.DataSource, DataTable))


MyConnection.Close()


MessageBox.Show("数据导出导入操作完成!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


Catch ex As Exception


MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Try


End Sub


End Class



三、用户登陆验证


'较安全的用户校验




Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Dim MyUserName As String = Me.TextBox1.Text


Dim MyPassword As String = Me.TextBox2.Text


Dim MySQL As String = "Select * From MyUsers Where UserName LIKE '" + MyUserName.Replace("'", "''") + "' And Password LIKE'" + MyPassword.Replace("'", "''") + "'"


Try


Dim MyConnection As New SqlConnection("Data Source=.;Integrated Security=SSPI; Database=Northwind;Asynchronous Processing=true")


Dim MyCommand As New SqlCommand(MySQL, MyConnection)


MyConnection.Open()


Dim MyReader As SqlDataReader = MyCommand.ExecuteReader()


If MyReader.Read() Then


MessageBox.Show("登录成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


Else


MessageBox.Show("登录失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End If


Catch ex As Exception


MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Try


End Sub

三、筛选记录




Public Class Form1Class Form1


'显示客户数据表




Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Try


Me.DataSet1 = New DataSet()


Me.OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "Northwind.mdb"


Me.OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM 客户"


Me.OleDbDataAdapter1.SelectCommand.Connection = Me.OleDbConnection1


Me.OleDbDataAdapter1.Fill(Me.DataSet1, "客户")


Me.DataGrid1.DataSource = Me.DataSet1.Tables(0)


Catch ex As Exception


MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Try


End Sub


'筛选排序数据信息




Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Try


Dim MyFilter As String = Me.TextBox1.Text '如:城市 like '%北京%'


Dim MySorter As String = Me.TextBox2.Text '如:公司名称 desc


Dim MyRows() As DataRow = Me.DataSet1.Tables(0).Select(MyFilter, MySorter)


Dim MyDataSet As New DataSet()


MyDataSet = Me.DataSet1.Clone()


MyDataSet.Tables(0).Rows.Clear()


For Each MyRow As DataRow In MyRows


Dim NewRow As DataRow = MyDataSet.Tables(0).NewRow


NewRow.Item("客户ID") = MyRow.Item("客户ID")


NewRow.Item("公司名称") = MyRow.Item("公司名称")


NewRow.Item("联系人姓名") = MyRow.Item("联系人姓名")


NewRow.Item("联系人头衔") = MyRow.Item("联系人头衔")


NewRow.Item("地址") = MyRow.Item("地址")


NewRow.Item("城市") = MyRow.Item("城市")


NewRow.Item("地区") = MyRow.Item("地区")


NewRow.Item("邮政编码") = MyRow.Item("邮政编码")


NewRow.Item("国家") = MyRow.Item("国家")


NewRow.Item("电话") = MyRow.Item("电话")


NewRow.Item("传真") = MyRow.Item("传真")


MyDataSet.Tables(0).Rows.Add(NewRow)


Next


Me.DataGrid1.DataSource = MyDataSet.Tables(0)


Catch ex As Exception


MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Try


End Sub


End Class

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: