C# 访问 sql Delphi oledb 数据库
this.constr1.Text = "Provider=SQLOLEDB;Data Source=10.0.8.6;Initial Catalog=toolrepair;User ID=sa;Pass" +
"word=2R00T@KF*TE;Auto Translate=false";
private void button1_Click(object sender, EventArgs e) { DataTable delphiTable = GetOleDbDataTable(search1.Text, "delphiTable", constr1.Text);
SqlCommand l_com = new SqlCommand("select * from " + tablename2.Text, new SqlConnection(constr2.Text)); SqlDataAdapter l_da = new SqlDataAdapter(l_com); SqlCommandBuilder l_sb = new SqlCommandBuilder(l_da); DataTable l_dtbl_struct = new DataTable(); l_da.Fill(l_dtbl_struct); DataRow newRow; foreach (DataRow dr in delphiTable.Rows) { newRow = l_dtbl_struct.NewRow(); foreach (DataColumn l_dc in l_dtbl_struct.Columns) { if (delphiTable.Columns.Contains(l_dc.ColumnName)) newRow[l_dc.ColumnName] = dr[l_dc.ColumnName]; } l_dtbl_struct.Rows.Add(newRow); } l_da.Update(l_dtbl_struct);//搜索Rowsate的狀態并執行 }
public DataTable GetOleDbDataTable(string pi_strSql, string pi_strTableName, string pi_strCnn) { DataTable l_dtbl_return = new DataTable("return"); OleDbConnection l_conn = new OleDbConnection(pi_strCnn); try { if (l_conn.State == ConnectionState.Closed) l_conn.Open(); OleDbDataAdapter l_da = new OleDbDataAdapter(pi_strSql, l_conn); l_da.Fill(l_dtbl_return); } finally { l_conn.Close(); } return l_dtbl_return; }