这次试验比较纠结,需要写一个可访问多数据表的可视化程序。vs2010这个平台非常庞大,安装也非常的麻烦,不会安装的同学可以看这里。
http://blog.csdn.net/liushuaikobe/article/details/7348238 我的小笔记本因为硬盘空间空间不够,没法装vs2010这个东西,所以只好把很多珍藏的xx删掉·······
言归正传,.net访问数据库是这样的。
首先要构造一个 String 里面包含了 用户名 密码 服务器名 数据库名 这些东西
一个正确的实例是这样的
str = "server=OUYANG-PC ;UID=sa ;PWD=12345qwert ;DataBase=test_database";conn的声明是这样
private SqlConnection conn;剩下的只要简单地连接一下就好了
public DataBaseConn() { str = "server=" + SERVERNAME + ";UID=" +UID+ ";PWD="+PWD+";DataBase="+DATABASENAME; conn = new SqlConnection(str); conn.Open(); }连接数据库后,要把数据库里的数据读取出来放到C#的一个控件DataGridView里,先看一下怎么执行SQL命令
SqlCommand cmd = new SqlCommand(sqlCommand, conn); return cmd.ExecuteNonQuery();上面乱七八糟的东西都是在构造一个删除的SQL语句
public int Del(List<string> fieldName,List<string> value) { string sqlCommand = "DELETE FROM "+TABLENAME+" WHERE "; sqlCommand += fieldName[0] + "=" + value[0]; for (int index = 1; index < fieldName.Count; index++) { sqlCommand += "and " + fieldName[index] + "=" + value[index]; } MessageBox.Show(sqlCommand); SqlCommand cmd = new SqlCommand(sqlCommand, conn); return cmd.ExecuteNonQuery(); }把数据放进DataGridView有两种方法SqlDataReader和DataSet
try { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "SELECT * FROM table_1"; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { for (int index = 0; index < dr.FieldCount; index++) { MessageBox.Show(dr.GetValue(index).ToString()); } } } catch (Exception) { }使用dataSet这个东西是把整个数据读进内存,然后在慢慢访问。
public DataSet GetData() { string sqlCommand = "SELECT * FROM "+TABLENAME; SqlCommand cmd = new SqlCommand(sqlCommand,conn); DataSet dataset = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dataset); return dataset; }
DataTable table = dataset.Tables[0];先把DataGridView的段名设置好
foreach (DataColumn dc in table.Columns) { dataGridView1.Columns.Add(dc.ColumnName, dc.ColumnName); }这里就是往Cells里面填数据了
DataGridViewRow vr = new DataGridViewRow();向Row里填入Cells
foreach (DataGridViewColumn dc in dataGridView1.Columns) { vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell); vr.Cells[vr.Cells.Count-1].Value = dr[dc.Name]; }最后把Row Add到GridView里面就行了
foreach (DataRow dr in table.Rows) { DataGridViewRow vr = new DataGridViewRow(); foreach (DataGridViewColumn dc in dataGridView1.Columns) { vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell); vr.Cells[vr.Cells.Count-1].Value = dr[dc.Name]; } dataGridView1.Rows.Add(vr); }这个方法大体就是这样子,这样就可以吧数据库数据读到DataGridView中了。
public void initGrid(){ //MessageBox.Show(DataBaseConn.TABLENAME); dataGridView1.Columns.Clear(); DataSet dataset = conn.GetData(); DataTable table = dataset.Tables[0]; foreach (DataColumn dc in table.Columns) { dataGridView1.Columns.Add(dc.ColumnName, dc.ColumnName); } foreach (DataRow dr in table.Rows) { DataGridViewRow vr = new DataGridViewRow(); foreach (DataGridViewColumn dc in dataGridView1.Columns) { vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell); vr.Cells[vr.Cells.Count-1].Value = dr[dc.Name]; } dataGridView1.Rows.Add(vr); } foreach (DataGridViewColumn dc in dataGridView1.Columns) { columnsName.Add(dc.Name); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication4 { public class DataBaseConn { public static string SERVERNAME = "OUYANG-PC"; public static string UID = "sa"; public static string PWD = "nihaoa123"; public static string DATABASENAME = "test"; public static string TABLENAME = "student"; private string str; private SqlCommand cmd; private SqlConnection conn; public DataBaseConn() { str = "server=" + SERVERNAME + ";UID=" +UID+ ";PWD="+PWD+";DataBase="+DATABASENAME; conn = new SqlConnection(str); conn.Open(); } public int Insert(List<string> fieldName,List<string> value) { string sqlCommand = "insert into " + TABLENAME + " ("; sqlCommand += fieldName[0]; for (int index = 1; index < fieldName.Count; index++) { sqlCommand += "," + fieldName[index]; } sqlCommand += ") values("; sqlCommand += value[0]; for (int index = 1; index < value.Count; index++) { sqlCommand += ",'"+value[index] + "'"; } sqlCommand += ")"; MessageBox.Show(sqlCommand); SqlCommand cmd = new SqlCommand(sqlCommand, conn); return cmd.ExecuteNonQuery(); } public int Del(List<string> fieldName,List<string> value) { string sqlCommand = "DELETE FROM "+TABLENAME+" WHERE "; sqlCommand += fieldName[0] + "=" + value[0]; for (int index = 1; index < fieldName.Count; index++) { sqlCommand += "and " + fieldName[index] + "=" + value[index]; } MessageBox.Show(sqlCommand); SqlCommand cmd = new SqlCommand(sqlCommand, conn); return cmd.ExecuteNonQuery(); } public int Updata(List<string> fieldName, List<String> value ,List<String> orivalue) { string sqlCommand = "UPDATE " + TABLENAME + " SET "; sqlCommand += fieldName[0] + "='" + value[0]; for (int index = 1; index < fieldName.Count; index++) { sqlCommand += "',"+fieldName[index] + "='" + value[index]; } sqlCommand += "' WHERE " + fieldName[0] + "='" + orivalue[0] + "'"; for (int index = 1; index < orivalue.Count; index++) { sqlCommand += "and " + fieldName[index] + "='" + orivalue[index] + "'"; } MessageBox.Show(sqlCommand); SqlCommand cmd = new SqlCommand(sqlCommand, conn); return cmd.ExecuteNonQuery(); } public DataSet GetData() { string sqlCommand = "SELECT * FROM "+TABLENAME; SqlCommand cmd = new SqlCommand(sqlCommand,conn); DataSet dataset = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dataset); return dataset; } } }
因为比较懒不愿意写动态生成的Form所以只支持有4个段的表,切换不同的table在setting里面,以为懒得写文件操作setting里面的修改是不会保存的,要想真的改需要到DataBaseConn里面去改。在不同的机器上运行先要把UID PSD SERVERNAME之类的改了才行子form刷新父form的问题我是覆盖了子form的构造函数,把父form的实例传进去。最后做个广告,我们宿舍诚邀各种LOL,友谊赛。