½Î÷¹ã¸æÍø
±êÌâ:
C#¶Ôµ×²ãÊý¾Ý²Ù×÷µÄͨÓÃÀàÖð¸öÆÊÎö
[´òÓ¡±¾Ò³]
×÷Õß:
À¶ÐÅ×Ó
ʱ¼ä:
2008-12-24 11:59
±êÌâ:
C#¶Ôµ×²ãÊý¾Ý²Ù×÷µÄͨÓÃÀàÖð¸öÆÊÎö
¡¡¡¡using System; ¡¡¡¡using System.Data; ¡¡¡¡using System.Data.SqlClient; ¡¡¡¡using System.Windows.Forms; ¡¡¡¡ ¡¡¡¡namespace DataLibrary ¡¡¡¡{ ¡¡¡¡ /// ¡¡¡¡ /// Êý¾ÝÁ¬½ÓÉèÖà ¡¡¡¡ /// ¡¡¡¡ public class SqlConnect ¡¡¡¡ { ¡¡¡¡¡¡static string sqlconnstr; ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Á¬½Ó×Ö·û´® ¡¡¡¡¡¡/// ¡¡¡¡¡¡public string ConnString ¡¡¡¡¡¡{ ¡¡¡¡¡¡ set ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡sqlconnstr=value; ¡¡¡¡¡¡ } ¡¡¡¡¡¡ get ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡return sqlconnstr; ¡¡¡¡¡¡ } ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// È¡µÃÁ¬½Ó ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¡¡¡¡¡¡public SqlConnection GetSqlConnection() ¡¡¡¡¡¡{ ¡¡¡¡¡¡ SqlConnection _connection=null; ¡¡¡¡¡¡ if(sqlconnstr==null || sqlconnstr=="") ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡string SystemDir=System.Environment.SystemDirectory; ¡¡¡¡¡¡¡¡DisposalConductService DisposalConductService1= new DisposalConductService(SystemDir "\\CBERP_Dis.DLL") ; ¡¡¡¡¡¡¡¡sqlconnstr = "server=" DisposalConductService1.GetValue("ServerName","(local)") ¡¡¡¡¡¡¡¡ ";user id=" DisposalConductService1.GetValue("SqlAccount","sa") ¡¡¡¡¡¡¡¡ ";password=" DisposalConductService1.GetValue("PassWord","jiayu520") ¡¡¡¡¡¡¡¡ ";Database=" DisposalConductService1.GetValue("Database","CBERP"); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ try ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡_connection = new SqlConnection(sqlconnstr); ¡¡¡¡¡¡¡¡_connection.Open(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ catch(SqlException SE) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡if(SE.Number==17 || SE.Number==18456) ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡ if( MessageBox.Show("Êý¾Ý¿â·ÃÎʲÎÊýÉèÖò»ÕýÈ·,ÊÇ·ñ¶ÔÐèÒª¶ÔÆä½øÐÐÉèÖÃ?","Ìáʾ",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes) ¡¡¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡¡¡sqlconnstr=""; ¡¡¡¡¡¡¡¡¡¡DisposalConduct config = new DisposalConduct(); ¡¡¡¡¡¡¡¡¡¡config.ShowDialog(); ¡¡¡¡¡¡¡¡¡¡_connection=GetSqlConnection(); ¡¡¡¡¡¡¡¡ } ¡¡¡¡¡¡¡¡ else ¡¡¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡¡¡MessageBox.Show("½¨Á¢Êý¾Ý¿âÁ¬½Óʧ°Ü,³ÌÐò²»ÄܼÌÐøÔËÐÐ!","´íÎó",MessageBoxButtons.OK,MessageBoxIcon.Stop); ¡¡¡¡¡¡¡¡¡¡Application.ExitThread(); ¡¡¡¡¡¡¡¡¡¡Application.Exit(); ¡¡¡¡¡¡¡¡ } ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡else ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡ MessageBox.Show(SE.Message,"ÌáʾÐÅÏ¢",MessageBoxButtons.OK,MessageBoxIcon.Information); ¡¡¡¡¡¡¡¡ _connection.Close(); ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡ } ¡¡¡¡¡¡ return _connection; ¡¡¡¡¡¡} ¡¡¡¡ } ¡¡¡¡ ¡¡¡¡ /// ¡¡¡¡ /// Êý¾ÝÅäÖà ¡¡¡¡ /// ¡¡¡¡ public class DataCatena : IDisposable ¡¡¡¡ { ¡¡¡¡¡¡private SqlConnection _connection; ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Êý¾ÝÅäÖà ¡¡¡¡¡¡/// ¡¡¡¡¡¡public DataCatena() ¡¡¡¡¡¡{ ¡¡¡¡¡¡ SqlConnect sqlconnect = new SqlConnect(); ¡¡¡¡¡¡ _connection = sqlconnect.GetSqlConnection(); ¡¡¡¡¡¡ _connection.Close(); ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Êý¾ÝÅäÖà ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Á¬½Ó×Ö·û´® ¡¡¡¡¡¡public DataCatena(String connStr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ _connection = new SqlConnection(connStr); ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// È¡µÃDataView ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Á¬½Ó×Ö·û´® ¡¡¡¡¡¡/// Êý¾ÝÊÓͼ ¡¡¡¡¡¡public DataView GetDataView(String Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ DataView dv=null; ¡¡¡¡¡¡ SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection); ¡¡¡¡¡¡ DataSet ds = new DataSet(); ¡¡¡¡¡¡ try ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡myCommand.Fill(ds,"table"); ¡¡¡¡¡¡¡¡dv=ds.Tables["table"].DefaultView; ¡¡¡¡¡¡ } ¡¡¡¡¡¡ catch(SqlException exc) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡DebugBox db = new DebugBox(Sqlstr "\r\n" exc.Message); ¡¡¡¡¡¡¡¡db.Show(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ return dv; ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ÇåÀí×ÊÔ´ ¡¡¡¡¡¡/// ¡¡¡¡¡¡public void Dispose() ¡¡¡¡¡¡{ ¡¡¡¡¡¡ Dispose(true); ¡¡¡¡¡¡ GC.SuppressFinalize(true); ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ÇåÀíËùÓÐÕýÔÚʹÓõÄ×ÊÔ´¡£ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¡¡¡¡¡¡protected virtual void Dispose(bool disposing) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ if (! disposing) ¡¡¡¡¡¡¡¡return; ¡¡¡¡ ¡¡¡¡¡¡ if (this._connection != null) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡this._connection.Dispose(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// È¡µÃÊý¾ÝÊÊÅäÆ÷ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// SQLÓï¾ä ¡¡¡¡¡¡/// Êý¾ÝÊÊÅäÆ÷ ¡¡¡¡¡¡public SqlDataAdapter GetSqlDA(string Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection); ¡¡¡¡¡¡ return myCommand; ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// È¡µÃÊý¾Ý±í ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// SQLÓï¾ä ¡¡¡¡¡¡/// Êý¾Ý±í ¡¡¡¡¡¡public DataTable GetDT(String Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection); ¡¡¡¡¡¡ DataSet ds = new DataSet(); ¡¡¡¡¡¡ try ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡myCommand.Fill(ds,"table"); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ catch(SqlException exc) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡DebugBox db = new DebugBox(Sqlstr "\r\n" exc.ToString()); ¡¡¡¡¡¡¡¡db.Show(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ return ds.Tables["table"]; ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// Ö´ÐÐÓï¾ä ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// SQLÓï¾ä ¡¡¡¡¡¡/// ¡¡¡¡¡¡public object ExecWithValue(String Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ object result=null; ¡¡¡¡¡¡ _connection.Open(); ¡¡¡¡¡¡ SqlCommand myCommand = new SqlCommand(Sqlstr, _connection); ¡¡¡¡¡¡ try ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡result=myCommand.ExecuteScalar(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ catch (SqlException exc) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡DebugBox db = new DebugBox(Sqlstr "\r\n" exc.ToString()); ¡¡¡¡¡¡¡¡db.Show(); ¡¡¡¡¡¡¡¡//MessageBox.Show(exc.Message); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ _connection.Close(); ¡¡¡¡¡¡ return result; ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ·µ»ØÏàÓ¦µÄÐÐÊý ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// SQLÓï¾ä ¡¡¡¡¡¡/// ÐÐÊý ¡¡¡¡¡¡public int Exec(String Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ int sucess=0;¡¡¡¡¡¡ ¡¡¡¡¡¡ SqlCommand myCommand = new SqlCommand(Sqlstr, _connection); ¡¡¡¡¡¡ _connection.Open(); ¡¡¡¡¡¡ try ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡myCommand.ExecuteNonQuery(); ¡¡¡¡¡¡ } ¡¡¡¡¡¡ catch (SqlException exc) ¡¡¡¡¡¡ { ¡¡¡¡¡¡¡¡DebugBox db = new DebugBox(Sqlstr "\r\n" exc.ToString()); ¡¡¡¡¡¡¡¡db.Show(); ¡¡¡¡¡¡¡¡//¡¡¡¡MessageBox.Show(exc.Message); ¡¡¡¡¡¡¡¡sucess=exc.Number; ¡¡¡¡¡¡ } ¡¡¡¡¡¡ _connection.Close(); ¡¡¡¡¡¡ return sucess; ¡¡¡¡¡¡} ¡¡¡¡ } ¡¡¡¡ ¡¡¡¡ /// ¡¡¡¡ /// µ¥±í²Ù×÷ ¡¡¡¡ /// ¡¡¡¡ public class SingleTable ¡¡¡¡ { ¡¡¡¡¡¡private DataCatena DataCatena; ¡¡¡¡¡¡private SqlDataAdapter myDataAdapter; ¡¡¡¡¡¡private DataSet ds; ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// µ¥±í²Ù×÷ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¡¡¡¡¡¡public SingleTable(string Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ DataCatena = new DataCatena(); ¡¡¡¡¡¡ myDataAdapter = DataCatena.GetSqlDA(Sqlstr); ¡¡¡¡¡¡ ds = new DataSet(); ¡¡¡¡¡¡ myDataAdapter.Fill(ds, "Table1"); ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¸üÐÂÒ»¸ö±í ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¡¡¡¡¡¡public void SetUpdateCommand(string Sqlstr) ¡¡¡¡¡¡{ ¡¡¡¡¡¡ myDataAdapter.UpdateCommand = new SqlCommand(Sqlstr,myDataAdapter.SelectCommand.Connection); ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ·µ»ØÊý¾Ý±í ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ¡¡¡¡¡¡public DataTable GetDataTable() ¡¡¡¡¡¡{ ¡¡¡¡¡¡ return ds.Tables["Table1"]; ¡¡¡¡¡¡} ¡¡¡¡ ¡¡¡¡¡¡/// ¡¡¡¡¡¡/// ·µ»ØÊý¾ÝÊÊÅäÆ÷ ¡¡¡¡¡¡/// ¡¡¡¡¡¡///
»¶Ó¹âÁÙ ½Î÷¹ã¸æÍø (http://bbs.jxadw.com/)
Powered by Discuz! X3.2