SqlConnection, the common method of Sqltransaction,sqlcommand

Source: Internet
Author: User

Using system;using system.data;using system.data.sqlclient;using system.text;namespace ConsoleApplication1{Static        Class Test8 {static SqlConnection cn;                Static SqlTransaction tr; public static void Test () {String dbConnection = "Data source=192.168.131.38;initial Catalog=swsk_yosan _161115;persist Security Info=true;user id=sa;password=s3300859!;            Multipleactiveresultsets=true; ";            cn = New SqlConnection (dbConnection); cn.            Open (); TR = CN.            BeginTransaction (); try {//to Update data by ExecuteNonQuery () updatabyexecutenonquery ("Edi_zenrecvn                O "," E0760002 ", 44444, DateTime.Now);                Getzenrecno ("E0760002", "Edi_zenrecvno");                Getbyexecutescalar ("E0760002", "Edi_zenrecvno");            TEST DataTable dt = Getdatabysqldataadapter (); } catch {tr.          Rollback ();  } finally {tr.commit (); if (CN! = NULL) CN.            Close (); }} private static void Updatabyexecutenonquery (String tblname, string kinouid, int uprecvno, DateTime upsy                Sdate) {try {SqlCommand cmupd = new SqlCommand ();                Cmupd.commandtimeout = 60;                Cmupd.connection = CN;                                cmupd.transaction = TR;                StringBuilder STB = new StringBuilder ();                Int32 RTN = 0; StB.                Appendline ("UPDATE" + tblname); StB.                Appendline ("SET edirecvno = @UPRECVNO"); StB.                Appendline (", Trkymd = @TRKYMD"); StB.                Appendline (", trktime = @TRKTIME"); StB.                Appendline ("WHERE kinokanriid = @KINOID"); Cmupd.commandtext = STB.                ToString ();        CMUPD.PARAMETERS.ADD ("@UPRECVNO", sqldbtype.bigint);        CMUPD.PARAMETERS.ADD ("@KINOID", SqlDbType.VarChar);                CMUPD.PARAMETERS.ADD ("@TRKYMD", sqldbtype.datetime);                CMUPD.PARAMETERS.ADD ("@TRKTIME", sqldbtype.datetime); cmupd.parameters["@UPRECVNO"].                Value = Uprecvno; cmupd.parameters["@KINOID"].                Value = Kinouid; cmupd.parameters["@TRKYMD"].                Value = upsysdate.date; cmupd.parameters["@TRKTIME"].                Value = upsysdate;                                RTN = Cmupd.executenonquery (); Cmupd.commandtext = STB.                                ToString ();                RTN = Cmupd.executenonquery (); Console.WriteLine (RTN.                ToString ());            Console.ReadLine ();            } catch (Exception ex) {throw ex; }} private static Int32 Getbyexecutenonquery (String kinouid, String tblname) {TR            y {datatable ret = new DataTable ();    SqlCommand cm = new SqlCommand ();                Cm.commandtimeout = 60; Cm.                Connection = CN;                SqlDataAdapter ADP = new SqlDataAdapter ();                DataSet ds = new DataSet ();                StringBuilder STB = new StringBuilder ();                Int32 RTN = 0; StB.                Appendline ("select Edirecvno"); StB.                Appendline ("from" + tblname); StB.                Appendline ("WHERE kinokanriid = '" + kinouid + "'"); StB.                Appendline ("ORDER by Trkymd, Trktime"); Cm.commandtext = STB.                ToString (); Cm.                Transaction = TR; Adp.                SelectCommand = cm; Adp.                Fill (ds, "getrecvt"); ret = ds.                tables["GETRECVT"]; if (ret. Rows.Count > 0) {rtn = Int. Parse (ret. Rows[0][0].                ToString ()); } Console.WriteLine (Rtn.                ToString ());                Console.ReadLine (); return RTN            } catch {throw;            }} private static string Getbyexecutescalar (String kinouid, String tblname) {try                    {SqlCommand cm = new SqlCommand {CommandTimeout = 60,                Connection = cn, Transaction = TR};                StringBuilder STB = new StringBuilder (); StB.                Appendline ("select Edirecvno"); StB.                Appendline ("from" + tblname); StB.                Appendline ("WHERE kinokanriid = '" + kinouid + "'"); StB.                Appendline ("ORDER by Trkymd, Trktime"); Cm.commandtext = STB.                ToString (); var rtn = cm.                                ExecuteScalar (); Console.WriteLine (RTN.                ToString ());                Console.ReadLine (); Return RTN.            ToString ();   } catch {throw;         }} private static DataTable Getdatabysqldataadapter () {var ds = new DataSet ();                var cm = new SqlCommand {CommandTimeout = $, Connection = cn,            Transaction = tr}; SQL Souel line のタイムアウトを using (var ADP = new SqlDataAdapter ()) {var STB = new Stringbuild                ER (); Blank のデータベースを get するSTB.                Appendline ("select *"); StB.                Appendline ("from" + "TABLENAME"); StB.                Appendline ("ORDER by Entrytime, SEQ, Mat_doc"); Cm.commandtext = STB.                ToString (); Adp.                SelectCommand = cm; Adp.            Fill (DS); } return DS.        Tables[0]; }    }   }

  

SqlConnection, the common method of Sqltransaction,sqlcommand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.