请问 winform 里的通用数据库操作函数应该怎么写
就是写在类里面,其他窗体把类实例化后调用其中的方法就可以很方便的发送SQL语句
------解决方案--------------------using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Text;
namespace 企业人事管理系统
{
class SQL_Link
{
#region 公共变量
SqlConnection SQL_Conn;
SqlDataAdapter DA;
SqlCommand CMD;
DataSet DS;
DataTable DT;
#endregion
#region 数据连接
public SqlConnection Link_SQL(string Server, string DataBase, string User_ID, string PassWord)
{
SQL_Conn = new SqlConnection(@ "server= " + Server + ";database= " + DataBase + ";user id= " + User_ID + ";pwd= " + PassWord);
return SQL_Conn;
}
#endregion
#region 数据查询
public DataSet SQL_Select(string Select_String)
{
DS = new DataSet();
DT = new DataTable();
try
{
DA = new SqlDataAdapter(Select_String, SQL_Conn);
DS.Clear();
DA.Fill(DS);
}
catch (Exception err)
{
try
{
int num = err.ToString().IndexOf( "。 ");
MessageBox.Show(err.ToString().Substring(1, num));
}
catch
{
MessageBox.Show(err.ToString());
}
}
return DS;
}
#endregion
#region 数据修改
public int SQL_Update(string Update_String)
{
CMD = new SqlCommand(Update_String, SQL_Conn);
int Result_Count = 0;
if (CMD.Connection.State.ToString() == "close ") SQL_Conn.Open();
try
{
SQL_Conn.Open();
Result_Count = CMD.ExecuteNonQuery();
}
catch (Exception err)
{
try
{
int num = err.ToString().IndexOf( "。 ");
MessageBox.Show(err.ToString().Substring(1, num));
}
catch
{
MessageBox.Show(err.ToString());
}
}
return Result_Count;
}
#endregion
#region 数据按条读取
public object Dr(string Read_String)
{
object Read_Data;
SQL_Conn.Open();
SqlDataReader dr = CMD.ExecuteReader();
if (dr.Read())
{
Read_Data = dr[Read_String];
}
return Read_Data;
}
#endregion
}
}
------解决方案--------------------下一个Enterpricesdata或SQLHELPER类