当前位置: 首页 > >

SQL异步执行技术

发布时间:

using System; using System.Collections.Generic; using System.Web; using System.Data.SqlClient; namespace MyDb { public class InputLogOnRecord { public InputLogOnRecord() { } private static string connectionStr = "Persist Security Info=False;Asynchronous Processing=true;Integrated Security=True;database=DB;server=(local)"; private static string recordTableName = "UserLoginRecord"; public static void Input(string[] inputArray) { SqlConnection sqlConnection = new SqlConnection(connectionStr); try { sqlConnection.Open(); } catch (Exception ex) { string errorStr = ex.Message; } SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; string cmdText = getRecordText(inputArray); sqlCommand.CommandText = cmdText; try { IAsyncResult asyncResult sqlCommand.BeginExecuteNonQuery(inputRecordAsync, sqlCommand); } catch (Exception ex) { =

string errorStr = ex.Message; sqlConnection.Close(); } } private static string getRecordText(string[] inputArray) { string cmdText = ""; foreach (string input in inputArray) { cmdText = cmdText + "'" + input + "',"; } cmdText = "insert into " + recordTableName + " values(" + cmdText.Substring(0, cmdText.Length - 1) + ")"; return cmdText; } private static void inputRecordAsync(IAsyncResult ar) { SqlCommand sqlCommand = ar.AsyncState as SqlCommand; int completeParam = sqlCommand.EndExecuteNonQuery(ar); sqlCommand.Connection.Close(); } } }




友情链接: