zoukankan      html  css  js  c++  java
  • 连接数据库代码

    OLEDB连接:(一)

    Imports System.Data

    Imports System.Data.OleDb

    ―――――――――――――――――

    Private strcon As String = "Provider=SQLOLEDB.1;Data Source=(local);

    Initial Catalog=数据库名;User ID=sa;Password=sa"

    Public DBcon As New OleDbConnection(strcon)

    Private ds As DataSet = New DataSet

    Private sqlStr As String

    ―――――――――――――――――

    DBcon.Open()

    sqlStr = "select * from 表"

    Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

    ds.Clear()

    adt.Fill(ds)

    Me.DataGrid1.DataSource = ds.Tables(0)  //在 DataGrid里显示

    DataBind()

    DBcon.Close()

    ―――――――――――――――――

    (其中“DataBind()”可以没有,根据具体情况而定)

     

    OLEDB连接:(二)

    Imports System.Data

    Imports System.Data.OleDb

    ―――――――――――――――――

    Private strcon As String = "Provider=SQLOLEDB.1;Data Source=(local);

    Initial Catalog=数据库名;User ID=sa;Password=sa"

    Public DBcon As New OleDbConnection(strcon)

    Private ds As DataSet = New DataSet

    Private sqlStr As String

    ―――――――――――――――――

    DBcon.Open()

    sqlStr = "select * from 表"

    Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

    ds.Clear()

    adt.Fill(ds)

    adt.ExecuteNonQuery()   //执行程序代码

    DBcon.Close()

    ―――――――――――――――――

    (其中“DataBind()”可以没有,根据具体情况而定)

     

    SQL连接

    Imports System.Data

    Imports System.Data.SqlClient

    ――――――――――――――――――

    Private strcon As String = "Data Source=(local);Initial Catalog=EDUDB;

    User ID=sa;Password=sa"

    Public DBcon As New SqlConnection(strcon)

    Private ds As DataSet = New DataSet

    Private sqlStr As String

    ―――――――――――――――――

    DBcon.Open()

    sqlStr = " select * from 表 "

    Dim adt As New SqlDataAdapter(sqlStr, DBcon)

    ds.Clear()

    adt.Fill(ds)

    dg.DataSource = ds.Tables(0)

    DBcon.Close()

    ------------------------------------

    另一种连接方法:(SQL连接)

    Imports System.Data

    Imports System.Data.SqlClient

    ―――――――――――――――

    Private conn As SqlConnection

    Private cmd As SqlCommand

    Dim conStr, sqlStr As String

    conStr = "Data Source=数据源;Initial Catalog=数据库名;User ID=sa;Password=sa"

    conn = New SqlConnection

    conn.ConnectionString = conStr

    cmd = conn.CreateCommand()

    cmd.CommandType = CommandType.Text

    ――――――――――

    sqlStr = "Insert into dbo.employee("

    sqlStr = sqlStr & "ID,Name,BirthDate,Sex,Tel,HP,Address)"

    sqlStr = sqlStr & "values("

    sqlStr = sqlStr & "@ID,@Name,@BirthDate,@Sex,@Tel,@HP,@Address)"

                    (连接数据库的SQL语言)

    ――――――――――

    cmd.CommandText = sqlStr

  • 相关阅读:
    各種語系的unicode對應以及local編碼方式
    测试邮箱采集
    devexpress控件例子
    2015结束,2016开始
    MYSQL 导出表结构
    mysql 远程访问
    jQuery的选择器中的通配符[id^='code']
    jquery判断checkbox是否选中及改变checkbox状态
    MYSQL 删除重复记录,只保留最大ID
    java.lang.IllegalArgumentException: Comparison method violates its general contract!
  • 原文地址:https://www.cnblogs.com/sishierfei/p/1610380.html
Copyright © 2011-2022 走看看