sql dataGrid连接数据库
————————————————————————————
using System.Data.SqlClient;
namespace sqlconnect3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.SqlClient.SqlCommand s qlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
SqlConnection con=new SqlConnection(this.sqlConnection1.ConnectionString);
SqlDataAdapter Dgb=new SqlDataAdapter("Select * From Address",con);
DataSet ds=new DataSet();
Dgb.Fill(ds,"Address");
dataGrid1.DataSource=ds.Tables["Address"].DefaultView;
}
oracle 连接
——————————————————————————————
using System;
using System.Data;
using System.Data.OleDb;
class OracleTest
{
static void Main()
{
const string strConn = "PROVIDER=OraOLEDB.Oracle;DATA SOURCE=YourDataSrcName;USER ID=YourUID;PASSWORD=YourPassword;PLSQLRSet=1";
const string strSQL = "Select * From YourTable";
try
{
OleDbConnection dbConn = new OleDbConnection(strConn);
DataSet ds = new DataSet("EmployeesSet");
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,dbConn);
da.Fill(ds,"Buyer");
Console.WriteLine(ds.GetXml());
}
catch(OleDbException e)
{
Console.WriteLine("Exception Occured :{0}",e.Errors[0].Message);
}
}
}
1:SqlServer连本地数据库
SqlConnection myConnection = new SqlConnection("server=(local)//NetSDK;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Titles");
MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;
MyRepeater.DataBind();
2:SqlServer连服务器绑定DataGrid连接
SqlConnection myConnection;
myConnection = new SqlConnection (" user id=;password=;initial catalog=MMAS;data source=MMASS;Connect Timeout=30");
myConnection.Open ();
string strSel="select * from bas_item ";
SqlDataAdapter myAdapter= new SqlDataAdapter (strSel,myConnection);
DataSet ds=new DataSet ();
myAdapter.Fill (ds,"bas_item");
DataGrid1.DataSource =ds.Tables ["bas_item"].DefaultView;
DataGrid1.DataBind ();
3:Access连接
string MyConnString="provider=Microsoft.Jet.OLEDB.4.0;Data Source=d://demo//db1.mdb";// "+Server.MapPath("db1.mdb")+"";
MyConn= new OleDbConnection(MyConnString);
MyConn.Open();
string strSel = "select * from u";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,"u");
DataGrid2.DataSource = ds.Tables ["u"].DefaultView
DataGrid2.DataBind();
目前在c#里两个连接类:SqlConnection与OleDbConnection.
两个类的例程分别如下:(from msdn)
1.OleDbConnection:
public void InsertRow(string myConnectionString)
{
// If the connection string is null, use a default.
if(myConnectionString == "")
{
myConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;" +
"Integrated Security=SSPI;";
}
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
2.SqlConnection:
public void InsertRow(string myConnectionString)
{
// If the connection string is null, use a default.
if(myConnectionString == "")
{
myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
}
SqlConnection myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
注:
SqlConnection针对于Sql server 7.0及以后版本。
OleDbConnection用于其他数据库。
至于myConnectinString里的provider参数,针对不同的数据源有:
(1)driver da microsoft para arquivos textto(*.txt,*.csv)
(2)driver do microsoft access(*.mdb)
(3)driver do microsoft dbase(*.dbf)
(4)driver do microsoft execl(*.xls)
(5)driver do microsoft paradox(*.db)
(6)Microsoft Access Driver(*.mdb)
(7)Microsoft Access -treiber(*.mdb)
(8)Microsoft Excel driver(*.xls)