sql dataGrid connected database ---------------------------- using System.Data.SqlClient;
namespace sqlconnect3 {///
Public form1 () {/// Required for Windows Form Designer Support // InitializationComponent ();
// // Todo: add any constructor code after initializecomponent call // sqlConnection con = New SqlConnection (this.sqlconnection1.connections;
SqlDataAdapter DGB = New SqldataAdapter ("Select * from address", con); dataset ds = new dataset (); DGB.FILL (DS, "Address");
DataGrid1.datasource = DS.TABLES ["address"]. defaultview;}
Oracle Connection --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 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 even local database 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.database (); 2: SQLServer Connection server binding DataGrid connection 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.database (); 3: Access Connect string myconnstring = "provider = microsoft.jet.Oledb.4.0; data source = d: //demo//db1.mdb"; // " server.mappath (" db1.mdb ") " "; myconn = New oledbconnection; myconn.open (); string strasel = "SELECT * F rom u "; DataSet ds = new DataSet (); OleDbDataAdapter MyAdapter = new OleDbDataAdapter (strSel, MyConn); MyAdapter.Fill (ds," u ");. DataGrid2.DataSource = ds.Tables [" u "] DefaultViewDataGrid2.DataBind ();
Currently in C #, two connectivity classes: SQLConnection and OLEDBConnection. Two classes of routines are as follows: (from msdn) 1.OLEDbConnection: Public void insertion (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 ();} Note: SQLConnection is for SQL Server 7.0 and later. OLEDBConnection is used for other databases.