SQL类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace Medecins.类包
{
class SQL
{
public static string conStr = "server=.;database=MZ_doctor;uid=sa;pwd=123";
public static string connectDB = "server.;database=MZ_doctor;uid=sa;pwd=123";
public static SqlConnection cn = new SqlConnection(connectDB);
public static DataSet GetDataFormDB(string sqlStr)
{
cn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlStr, cn);
DataSet myDataSet = new DataSet();
myDataSet.Clear();
myAdapter.Fill(myDataSet);
cn.Close();
if (myDataSet.Tables[0].Rows.Count != 0)
{
return myDataSet;
}
else
{
return null;
}
}
//“写”数据库的静态方法
public static bool UpdateDB(string sqlStr)
{
cn.Open();
//定义数据命令对象
SqlCommand myCmd = new SqlCommand(sqlStr, cn);
//设置Command对象的CommandType属性
myCmd.CommandType = CommandType.Text;
//执行SQL语句
myCmd.ExecuteNonQuery();
cn.Close();
//数据更新成功,返回true
return true;
}
}
}页面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Internal.Performance;
using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;//RegEx是Visual Studio .NET中的正则表达式类
using Medecins.类包;namespace Medecins.数据初始化
{
public partial class 人员管理 : Form
{
DataSet ds = new DataSet(); public 人员管理()
{
InitializeComponent();
}
void rfcs()
{
string ryxx="select RY_ID AS 工号,RY_XM AS 姓名,RY_JM AS 简码,RY_BM AS 民族 ,RY_XL AS 学历,RY_CSRQ AS 出生日期,RY_SFZH AS 身份证号,RY_GLZW AS 管理职务"; ds = SQL.GetDataFormDB(ryxx);
if (ds != null)
{
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns[0].HeaderText = "工号";
dataGridView1.Columns[0].Width = 60; dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[1].Width = 60;
}
else
{
dataGridView1.DataSource = null;
} }
#region //数据控件的RowHeaderMouseClick 事件代码
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
int n = dataGridView1.CurrentCell.RowIndex;
if (n > 0 && n < dataGridView1.RowCount - 1)
{
textBox1.Text = this.dataGridView1[0, n].Value.ToString().Trim();
textBox2.Text = dataGridView1[1, n].Value.ToString().Trim();
textBox3.Text = dataGridView1[2, n].Value.ToString().Trim();
textBox4.Text = dataGridView1[3, n].Value.ToString().Trim();
textBox5.Text = dataGridView1[4, n].Value.ToString().Trim();
textBox6.Text = dataGridView1[5, n].Value.ToString().Trim();
textBox7.Text = dataGridView1[6, n].Value.ToString().Trim();
textBox8.Text = dataGridView1[7, n].Value.ToString().Trim();
textBox9.Text = dataGridView1[8, n].Value.ToString().Trim();
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion #region//DataGridView点击触发事件
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
try
{
//避免选中列标题,报错
if (e.RowIndex.ToString() != "-1")
{
//单击某一单元格获取当前的行赋于字段值
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["工号"].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["姓名"].Value.ToString();
//pictureBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["相片"].Value.ToString();
}
if (textBox1.Text.ToString() != "")
{
button4.Enabled = true;
}
else
{
button4.Enabled = false;
}
}
catch (Exception)
{
MessageBox.Show("操作有误,请检查!", "温馨提示");
}
}
#endregion #region 增加人员信息
private void button2_Click(object sender, EventArgs e)
{
人员管理_增加人员 add_ry = new 人员管理_增加人员();
add_ry.Show(); }
#endregion private void button1_Click(object sender, EventArgs e)
{
try
{
string CX=(SQL.connectDB);
if (textBox10.Text.Trim() == "")
{
MessageBox.Show("请输入需要查询的工号或名称!","提示");
return;
}
else
{
CX="select RY_ID,RY_Xm from ZD_RY";
}
DataSet ds=new DataSet();
ds=SQL.GetDataFormDB(CX);
if (ds!=null)
{
dataGridView1.DataSource=ds.Tables[0];
dataGridView1.Columns[0].HeaderText="工号";
dataGridView1.Columns[0].Width=60;
}
else
{
dataGridView1.DataSource=null;
MessageBox.Show("错误!");
}
}
catch(Exception ex)
{
SQL.cn.Close();
MessageBox.Show(ex.Message);
}
}
}
}抛出的异常提示在:DataGridView
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace Medecins.类包
{
class SQL
{
public static string conStr = "server=.;database=MZ_doctor;uid=sa;pwd=123";
public static string connectDB = "server.;database=MZ_doctor;uid=sa;pwd=123";
public static SqlConnection cn = new SqlConnection(connectDB);
public static DataSet GetDataFormDB(string sqlStr)
{
cn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlStr, cn);
DataSet myDataSet = new DataSet();
myDataSet.Clear();
myAdapter.Fill(myDataSet);
cn.Close();
if (myDataSet.Tables[0].Rows.Count != 0)
{
return myDataSet;
}
else
{
return null;
}
}
//“写”数据库的静态方法
public static bool UpdateDB(string sqlStr)
{
cn.Open();
//定义数据命令对象
SqlCommand myCmd = new SqlCommand(sqlStr, cn);
//设置Command对象的CommandType属性
myCmd.CommandType = CommandType.Text;
//执行SQL语句
myCmd.ExecuteNonQuery();
cn.Close();
//数据更新成功,返回true
return true;
}
}
}页面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Internal.Performance;
using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;//RegEx是Visual Studio .NET中的正则表达式类
using Medecins.类包;namespace Medecins.数据初始化
{
public partial class 人员管理 : Form
{
DataSet ds = new DataSet(); public 人员管理()
{
InitializeComponent();
}
void rfcs()
{
string ryxx="select RY_ID AS 工号,RY_XM AS 姓名,RY_JM AS 简码,RY_BM AS 民族 ,RY_XL AS 学历,RY_CSRQ AS 出生日期,RY_SFZH AS 身份证号,RY_GLZW AS 管理职务"; ds = SQL.GetDataFormDB(ryxx);
if (ds != null)
{
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns[0].HeaderText = "工号";
dataGridView1.Columns[0].Width = 60; dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[1].Width = 60;
}
else
{
dataGridView1.DataSource = null;
} }
#region //数据控件的RowHeaderMouseClick 事件代码
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
int n = dataGridView1.CurrentCell.RowIndex;
if (n > 0 && n < dataGridView1.RowCount - 1)
{
textBox1.Text = this.dataGridView1[0, n].Value.ToString().Trim();
textBox2.Text = dataGridView1[1, n].Value.ToString().Trim();
textBox3.Text = dataGridView1[2, n].Value.ToString().Trim();
textBox4.Text = dataGridView1[3, n].Value.ToString().Trim();
textBox5.Text = dataGridView1[4, n].Value.ToString().Trim();
textBox6.Text = dataGridView1[5, n].Value.ToString().Trim();
textBox7.Text = dataGridView1[6, n].Value.ToString().Trim();
textBox8.Text = dataGridView1[7, n].Value.ToString().Trim();
textBox9.Text = dataGridView1[8, n].Value.ToString().Trim();
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion #region//DataGridView点击触发事件
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
try
{
//避免选中列标题,报错
if (e.RowIndex.ToString() != "-1")
{
//单击某一单元格获取当前的行赋于字段值
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["工号"].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["姓名"].Value.ToString();
//pictureBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["相片"].Value.ToString();
}
if (textBox1.Text.ToString() != "")
{
button4.Enabled = true;
}
else
{
button4.Enabled = false;
}
}
catch (Exception)
{
MessageBox.Show("操作有误,请检查!", "温馨提示");
}
}
#endregion #region 增加人员信息
private void button2_Click(object sender, EventArgs e)
{
人员管理_增加人员 add_ry = new 人员管理_增加人员();
add_ry.Show(); }
#endregion private void button1_Click(object sender, EventArgs e)
{
try
{
string CX=(SQL.connectDB);
if (textBox10.Text.Trim() == "")
{
MessageBox.Show("请输入需要查询的工号或名称!","提示");
return;
}
else
{
CX="select RY_ID,RY_Xm from ZD_RY";
}
DataSet ds=new DataSet();
ds=SQL.GetDataFormDB(CX);
if (ds!=null)
{
dataGridView1.DataSource=ds.Tables[0];
dataGridView1.Columns[0].HeaderText="工号";
dataGridView1.Columns[0].Width=60;
}
else
{
dataGridView1.DataSource=null;
MessageBox.Show("错误!");
}
}
catch(Exception ex)
{
SQL.cn.Close();
MessageBox.Show(ex.Message);
}
}
}
}抛出的异常提示在:DataGridView
抛出异常求高手帮忙解答如何解决,谢谢!