本文整理汇总了C#中System.Data.SqlClient.SqlCommand.BeginExecuteReader方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.BeginExecuteReader方法的具体用法?C# SqlCommand.BeginExecuteReader怎么用?C# SqlCommand.BeginExecuteReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.BeginExecuteReader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AsyncConnect
public AsyncConnect()
{
SqlConnection connection = new SqlConnection();
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder();
connectionBuilder.DataSource = @"(local)\SQLEXPRESS";
connectionBuilder.IntegratedSecurity = true;
connectionBuilder.AsynchronousProcessing = true;
connectionBuilder.InitialCatalog = "AutoLot";
connection.ConnectionString = connectionBuilder.ConnectionString;
connection.Open();
string query = "WaitFor Delay '00:00:05';Select * From Inventory";
SqlCommand command = new SqlCommand(query, connection);
IAsyncResult connectionResult = command.BeginExecuteReader(CommandBehavior.CloseConnection);
while (!connectionResult.IsCompleted)
{
Console.WriteLine("Working on Main thread...");
Thread.Sleep(1000);
}
Console.WriteLine();
SqlDataReader reader = command.EndExecuteReader(connectionResult);
Console.WriteLine(DbUtils.ReaderToString(reader));
}
示例2: BeginTask
private IAsyncResult BeginTask(object sender, EventArgs e, AsyncCallback cb, object state)
{
string connectionString = WebConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
con = new SqlConnection(connectionString);
cmd = new SqlCommand("SELECT * FROM Employees", con);
con.Open();
return cmd.BeginExecuteReader(cb, state);
}
示例3: BeginFindAll
public IAsyncResult BeginFindAll(AsyncCallback callback, Object asyncState)
{
var query = from w in _db.Widgets
select w;
_beginFindAllCmd = _db.GetCommand(query) as SqlCommand;
_db.Connection.Open();
return _beginFindAllCmd.BeginExecuteReader(callback, asyncState, System.Data.CommandBehavior.CloseConnection);
}
示例4: CheckCharacterTimestamp
/// <summary>
/// Checks when a client's characters were last cached, against a timestamp received from the client.
/// If the client's timestamp doesn't match the one in the DB (meaning it was older or newer), information
/// about all the characters is sent to the client.
/// </summary>
/// <param name="Timestamp">The timestamp received from the client.</param>
public static void CheckCharacterTimestamp(string AccountName, LoginClient Client, DateTime Timestamp)
{
SqlCommand Command = new SqlCommand("SELECT AccountName, NumCharacters, Character1, Character2, Character3 " +
"FROM Accounts");
Command.Connection = m_Connection;
Command.BeginExecuteReader(new AsyncCallback(EndCheckCharacterID),
new DatabaseAsyncObject(AccountName, ref Client, Timestamp, Command));
}
示例5: BeginExecute
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
{
sourceConnection =
new SqlConnection(connectionStringOrigem.Get(context));
sourceConnection.Open();
commandSourceData = new SqlCommand(Query.Get(context), sourceConnection);
var task =
commandSourceData.BeginExecuteReader(callback, state, System.Data.CommandBehavior.Default);
return task;
}
示例6: btnAsynchronousCall_Click
private void btnAsynchronousCall_Click(object sender, EventArgs e)
{
if (isExecuting)
{
MessageBox.Show(this, "Already executing. Please wait until " +
"the current query has completed.");
}
else
{
SqlCommand command = null;
try
{
DisplayStatus("Connecting...");
//To Execute the command asynchronously we need to make Asynchronous Processing=true
conn= new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;Asynchronous Processing=true");
command = new SqlCommand();
// To emulate a long-running query, wait for a few seconds before working with the data.
// This command doesn't do much, but that's the point--it doesn't change your data,
// in the long run.
command.CommandText = "WAITFOR DELAY '00:00:05' ; SELECT * FROM Customers";
command.Connection = conn;
conn.Open();
DisplayStatus("Executing...");
isExecuting = true;
AsyncCallback callback = new AsyncCallback(HandleCallback);
// Although it's not required that you pass the SqlCommand object as the second parameter
// in the BeginExecuteNonQuery call, doing so makes it easier to call EndExecuteNonQuery
// in the callback procedure.
// Once the BeginExecuteNonQuery method is called, the code continues--and the user can
// interact with the form--while the server executes the query.
command.BeginExecuteReader(callback, command);
}
catch (Exception ex)
{
isExecuting = false;
//DisplayStatus("Error: " + ex.Message);
DisplayStatus( string.Format("Ready (last error: {0})", ex.Message));
if (conn!= null)
{
conn.Close();
}
}
}
}
示例7: Execute
/// <summary>
/// 异步执行SQL
/// </summary>
/// <param name="sql"></param>
public void Execute(string sql)
{
if (string.IsNullOrEmpty(sql)) return;
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.BeginExecuteReader(new AsyncCallback((arg) =>
{
if (null != this.OnExecuteCompleted && arg.IsCompleted)
this.OnExecuteCompleted(this, new SqlAsyncExecuteResultArg
{
AsyncResult = arg
});
}), null,System.Data.CommandBehavior.CloseConnection);
}
示例8: AsyncExecute
/// <summary>
/// 异步执行SQL
/// </summary>
/// <param name="connstr"></param>
/// <param name="sql"></param>
/// <param name="cmdtype"></param>
/// <param name="prms"></param>
public static void AsyncExecute(string connstr, string sql,CommandType cmdtype, params SqlParameter[] prms)
{
SqlConnectionStringBuilder connBuild = new SqlConnectionStringBuilder(connstr);
connBuild.AsynchronousProcessing = true;
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(connBuild.ConnectionString);
cmd.CommandTimeout = 6000 * 100;
cmd.CommandType = cmdtype;
cmd.CommandText = sql;
if (null != prms)
cmd.Parameters.AddRange(prms.ToArray());
cmd.Connection.Open();
cmd.BeginExecuteReader(CommandBehavior.CloseConnection);
}
示例9: btnExecute_Click
private void btnExecute_Click(object sender, EventArgs e)
{
//string connString = "Async=true;SERVER=(local);DATABASE=northwind;UID=sa;";
string connString = "Async=true;SERVER=www.j2ee.ca;DATABASE=northwind;uid=aspx;password=aspx;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("SELECT lastname,firstname FROM employees", conn);
_cmd = cmd;
conn.Open();
AsyncCallback callback = new AsyncCallback(ProcessData);
IAsyncResult iar = cmd.BeginExecuteReader(callback, null);
iar.AsyncWaitHandle.WaitOne();
}
示例10: btnExecute_Click
private void btnExecute_Click(object sender, EventArgs e)
{
string connString = "Async=true;SERVER=(local);DATABASE=northwind;UID=sa;";
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand("SELECT lastname,firstname FROM employees", conn);
conn.Open();
IAsyncResult iar = cmd.BeginExecuteReader();
// Do something here
SqlDataReader reader = (SqlDataReader)cmd.EndExecuteReader(iar);
ProcessData(reader);
}
}
示例11: AsyncExecute
public static void AsyncExecute(string connstr, string sql, CommandType cmdtype, params SqlParameter[] prms)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connstr) {
AsynchronousProcessing = true
};
SqlCommand command = new SqlCommand {
Connection = new SqlConnection(builder.ConnectionString),
CommandTimeout = 0x927c0,
CommandType = cmdtype,
CommandText = sql
};
if (prms != null)
{
command.Parameters.AddRange(prms.ToArray<SqlParameter>());
}
command.Connection.Open();
command.BeginExecuteReader(CommandBehavior.CloseConnection);
}
示例12: CheckAccount
/// <summary>
/// Checks whether or not an account existed, and whether or not the password supplied was correct.
/// </summary>
/// <param name="AccountName">The name of the account.</param>
/// <param name="Client">The client that supplied the account.</param>
/// <param name="Hash">The hash of the password (with the username as a salt).</param>
public static void CheckAccount(string AccountName, LoginClient Client, byte[] Hash)
{
if (m_Connection == null)
{
if (GlobalSettings.Default.CreateAccountsOnLogin == false)
{
//TODO: Check if a flat file database exists, otherwise send an accountlogin failed packet.
}
else
{
//TODO: Write account into flat file DB if it doesn't exist.
}
}
//Gets the data from both rows (AccountName & Password)
SqlCommand Command = new SqlCommand("SELECT AccountName, Password FROM Accounts");
Command.Connection = m_Connection;
Command.BeginExecuteReader(new AsyncCallback(EndCheckAccountName),
new DatabaseAsyncObject(AccountName, ref Client, Command, Hash));
}
示例13: Consume
// Listen for outgoing message queue.
private static void Consume()
{
// pooling timeouts
const int listenCommandTimeoutValue = 10000;
const int listenCommandTimeout = listenCommandTimeoutValue + 10000;
// prepare call of sp to dequeue message from outgoing message queue
var command = new SqlCommand("ListenOut", connection)
{
CommandType = CommandType.StoredProcedure,
CommandTimeout = listenCommandTimeout
};
command.Parameters.AddRange(new[]
{
new SqlParameter("@timeout", SqlDbType.Int) { Value = listenCommandTimeoutValue },
new SqlParameter(listenCommandMessageBodyName, SqlDbType.Xml) { Direction = ParameterDirection.Output }
});
// wait for outgoing message
command.BeginExecuteReader(OnConsumeComplete, command);
}
示例14: EndCheckCharacterID
/// <summary>
/// Callback mehod for CheckCharacterTimestamp.
/// This queries for the existence of a particular account
/// in the DB and retrieves the character IDs associated with it.
/// </summary>
private static void EndCheckCharacterID(IAsyncResult AR)
{
DatabaseAsyncObject AsyncObject = AR.AsyncState as DatabaseAsyncObject;
bool FoundAccountName = false;
int NumCharacters = 0;
int CharacterID1 = 0;
int CharacterID2 = 0;
int CharacterID3 = 0;
using (SqlDataReader Reader = AsyncObject.Cmd.EndExecuteReader(AR))
{
while (Reader.Read())
{
if (((string)Reader[0]).ToUpper() == AsyncObject.AccountName.ToUpper())
{
FoundAccountName = true;
NumCharacters = (int)Reader[1];
if (NumCharacters == 0)
break;
else if (NumCharacters == 1)
CharacterID1 = (int)Reader[2];
else if (NumCharacters == 2)
{
CharacterID1 = (int)Reader[2];
CharacterID2 = (int)Reader[3];
}
else if (NumCharacters == 3)
{
CharacterID1 = (int)Reader[2];
CharacterID2 = (int)Reader[3];
CharacterID3 = (int)Reader[4];
}
if (FoundAccountName == true)
break;
}
}
}
if (FoundAccountName)
{
if (NumCharacters > 0)
{
SqlCommand Command = new SqlCommand("SELECT CharacterID, LastCached, Name, Sex FROM Character");
AsyncObject.NumCharacters = NumCharacters;
AsyncObject.CharacterID1 = CharacterID1;
AsyncObject.CharacterID2 = CharacterID2;
AsyncObject.CharacterID3 = CharacterID3;
Command.Connection = m_Connection;
Command.BeginExecuteReader(new AsyncCallback(EndCheckCharacterTimestamp), AsyncObject);
}
else
{
PacketStream Packet = new PacketStream(0x05, 0);
Packet.WriteByte(0x00); //0 characters.
AsyncObject.Client.SendEncrypted(0x05, Packet.ToArray());
}
}
}
示例15: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (Page.ClientQueryString == null)
Response.Redirect("Error.aspx");
// Get user info from SQL
// Query the DB
string queryStr = Request.QueryString.ToString();
SqlConnection sqlCon = new SqlConnection();
SqlCommand sqlComm = new SqlCommand();
sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
// Parse query string
queryStr = Page.Request.QueryString["name"].ToString();
// Assure user is themselves or is admin for edit purposes
bool canEdit;
if (User.Identity.Name.Equals(queryStr) || User.Identity.Name.Equals("admin") || User.Identity.Equals("admin2"))
canEdit = true;
else
canEdit = false;
// Begin building the SQL query to populated the Edit boxes
sqlComm.Parameters.Add("@Fname", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@MI", System.Data.SqlDbType.Char);
sqlComm.Parameters.Add("@Lname", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@PNum", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Street", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@State", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@ZIP", System.Data.SqlDbType.Char);
sqlComm.Parameters.Add("@School", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Degree", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Major", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Minor", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@GradDate", System.Data.SqlDbType.Date);
sqlComm.Parameters.Add("@GPA", System.Data.SqlDbType.Float);
sqlComm.Parameters.Add("@UEmail", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Employer", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpTitle", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@Sched", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpCtctInf", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpEmail", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpStrtDt", System.Data.SqlDbType.Date);
sqlComm.Parameters.Add("@EmpEndDt", System.Data.SqlDbType.Date);
sqlComm.Parameters.Add("@EmpHist", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpHistTitle", System.Data.SqlDbType.VarChar);
sqlComm.Parameters.Add("@EmpHistEmail", System.Data.SqlDbType.VarChar);
// SQL query here
sqlComm.BeginExecuteReader();
// Return results will go here
bool readOnly = !canEdit;
// Populate with values
FirstNameBox.Text = ""; // Some stuff to go here, can't leave it as just ""
MiddleInitialBox.Text = "";
LastNameBox.Text = "";
PhoneNumBox.Text = "";
StreetBox.Text = "";
CityBox.Text = "";
StateDropdown.Text = "";
ZIPBox.Text = "";
UniversityTextBox.Text = "";
DegreeDropdown.Text = "";
MajorDropdown.Text = "";
MinorDropdown.Text = "";
GPABox.Text = "";
GraduationMonth.Text = "";
GradYearDropdown.Text = "";
UniversityEmailBox.Text = "";
EmployerBox.Text = "";
EmployeeTitleBox.Text = "";
ScheduleBox.Text = "";
EmployerContactInfoBox.Text = "";
EmployerEmailBox.Text = "";
EmployerStartDateDDDay.Text = "";
EmployerStartDateDDMonth.Text = "";
EmployerStartDateDDYear.Text = "";
EmployerEndDateDay.Text = "";
EmployerEndDateMonth.Text = "";
EmployerEndDateYear.Text = "";
EmployerHistoryBox.Text = "";
EmployerHistoryTitleBox.Text = "";
// Read only?
FirstNameBox.ReadOnly = readOnly;
MiddleInitialBox.ReadOnly = readOnly;
LastNameBox.ReadOnly = readOnly;
PhoneNumBox.ReadOnly = readOnly;
StreetBox.ReadOnly = readOnly;
CityBox.ReadOnly = readOnly;
StateDropdown.Enabled = canEdit;
ZIPBox.ReadOnly = readOnly;
UniversityTextBox.ReadOnly = readOnly;
DegreeDropdown.Enabled = canEdit;
MajorDropdown.Enabled = canEdit;
MinorDropdown.Enabled = canEdit;
GPABox.ReadOnly = readOnly;
GraduationMonth.Enabled = canEdit;
//.........这里部分代码省略.........