数据库操作
<1>连接数据库操作:
string s = "Data Source=服务器名称;Initial Catalog=数据库名称;Integrated Security=True"; //连接数据库所需要的字符串
SqlConnection con = new SqlConnection();
con.ConnectionString = s;
con.Open();
<2>将数据库信息显示在datagridview中
string selectCommandText = "select * from 治疗信息(表名)";
DataSet ds = new DataSet();
con.ConnectionString = s;
sda = new SqlDataAdapter(selectCommandText, con);
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0]; //完成数据绑定,datagridview控件可以显示
<3>DataGridView动态更新数据库:
string s = "Data Source=A3135;Initial Catalog=TotalNumber;Integrated Security=True";
SqlConnection con = new SqlConnection();
SqlDataAdapter sda = new SqlDataAdapter();
BindingSource bs = new BindingSource();
DataTable dt = new DataTable();
绑定数据库按钮:
con.ConnectionString = s;
string selectCommandText = "select * from 数据源";
sda = new SqlDataAdapter(selectCommandText, con);
sda.Fill(dt);
bs.DataSource = dt; //绑定BindingSource
dataGridView1.DataSource = dt; //完成数据绑定,datagridview可以显示数据
动态更新数据库按钮:
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update((DataTable)bs.DataSource);
获取当前行的信息:
RowNumber = SearchAnswer.CurrentCell.RowIndex; //SearchAnswer为DataGridview名
SickNumber = (int)SearchAnswer.Rows[RowNumber].Cells[0].Value;
string name = (string)SearchAnswer.Rows[RowNumber].Cells[1].Value;
设置当前选中行为一整行:
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选中状态为当前行
设置当前列不可排序:
dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; //禁止对列进行排序
<4>DataGridView实现数据库的修改
删除选中行:
int RowNumber = dataGridView1.CurrentCell.RowIndex; //获取当前选中行的行号
dataGridView1.Rows.RemoveAt(RowNumber);
SqlCommandBuilder cb = new SqlCommandBuilder(sda); //更新数据库
sda.Update((DataTable)bs.DataSource);
动态添加行,显示DataTable中的内容:
private void SetupColumns(DataGridView dgv, DataTable dt)
{
dgv.AutoGenerateColumns = false; //设置datagridview不可自动创建列
//手动为datagridview添加新列
dgv.Columns.Clear();
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.HeaderText = "病历名称"; //列名称
nameColumn.ValueType = typeof(string); //设置列属性
dgv.Columns.Add(nameColumn); //添加列
DataGridViewTextBoxColumn CreateTimeColumn = new DataGridViewTextBoxColumn();
CreateTimeColumn.HeaderText = "创建时间";
CreateTimeColumn.ValueType = typeof(string);
dgv.Columns.Add(CreateTimeColumn);
DataGridViewTextBoxColumn PathColumn = new DataGridViewTextBoxColumn();
PathColumn.HeaderText = "病历保存路径";
PathColumn.ValueType = typeof(string);
dgv.Columns.Add(PathColumn);
//循环遍¦历Datatable中内容赋值给datagridview的每一行
foreach (DataRow dr in dt.Rows)
{
int j = 0;
int dgvCount = dgv.Rows.Add(); //给datagridview动态创建一个行对象
foreach (DataColumn dc in dt.Columns)
{
string name = (string)dr[dc];
dgv.Rows[dgvCount].Cells[j].Value = name;
j++;
}
}
}
<6>将数据库信息写入byte数组:
con.ConnectionString = s;
con.Open();
SqlCommand sqlcommon = new SqlCommand("select * from 数据源", con);
SqlDataReader reader1 = sqlcommon.ExecuteReader();
int Line = reader1.FieldCount - 1;//获取当前行中的列数
int Row = 0; //记录当前数据库的行数
int num = 0; //记录receiveData数据
while (reader1.Read())
{
Row++;
}
reader1.Close();
byte[] receiveData = new byte[Row * Line];
SqlDataReader reader = sqlcommon.ExecuteReader();
while (reader.Read())
{
receiveData[num] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 1] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 2] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 3] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 4] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 5] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
receiveData[num + 6] = (byte)(int.Parse((string)reader["列名"].ToString().Trim()));
num += 7;
}
reader.Close();
con.Close();
<7>将byte数组写入数据库:
con.Open();
string SqlStartTime = " insert into [表名](列1,列2,列3)values(@Line,@FH1,@FH2)";
SqlCommand cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("@Line", MajorKey);
cmd.Parameters.AddWithValue("@FH1", buffer[i].ToString());
cmd.Parameters.AddWithValue("@FH2", buffer[i + 1].ToString());
cmd.ExecuteNonQuery();//注意此句没有就无法插入数据
con.Close(); //关闭打开连接
<8>DropDownList动态显示数据库中的表:
string s = "Data Source=A3135;Initial Catalog=TotalNumber;Integrated Security=True";
con = new SqlConnection(s);
DataSet ds = new DataSet();
string strSQL = "select name from sysobjects where type='" + 'U' + "'";
sda = new SqlDataAdapter(strSQL, con);
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}