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