如何在单击同一按钮时更新和读取 sql 列的值
how to update and read the value of sql column on click of the same button
我正在做的是单击一个按钮,我正在通过向其添加 +1 来更新 sql 列。这样做我没有遇到任何问题。现在我想要的是,对于同一个按钮单击事件,我想读取我已更新的那一列的值,并将其在查询字符串中发送到另一个页面。这是代码-
int i=0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
sql.Open();
string r = "update counter set m_id=m_id+1 where id=1";
SqlCommand com = new SqlCommand(r, sql);
com.ExecuteNonQuery();
sql.Close();
{
SendHTMLMail();
}
void SendHTMLMail()
{
StreamReader reader = new StreamReader(dd1.SelectedItem.Value);
string readFile = reader.ReadToEnd();
Regex regx = new Regex("(?<!src=\")http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([a-zA-Z0-9\?\#\=\/]){1})?", RegexOptions.IgnoreCase);
string output = regx.ToString();
string count = 0.ToString();
output = readFile;
string username = Server.UrlEncode(this.txtUsername.Text);
//here i want to read the value of m_id that i have updated as shown above and setting it to 'i' and passing this i to the query string//
sql.Open();
string re = "select m_id from counter where id=1";
SqlCommand ccmd = new SqlCommand(re, sql);
com.ExecuteNonQuery();
SqlDataReader read = ccmd.ExecuteReader();
{
while (read.Read())
i =int.Parse(read["m_id"].ToString());
}
sql.Close();
output = regx.Replace(output, new MatchEvaluator((match) =>
{
var url = Uri.EscapeDataString(match.Value.ToString());
return $"http://localhost:61187/two?sender={username}&link={url}&count={count}&mailer_id={i}";
}));
现在,问题不是从 5 更新到 6,而是 m_id 更新到 7
您可以批处理 命令,只执行一个命令而不是调用数据库两次
protected void btnSubmit_Click(object sender, EventArgs e)
{
sql.Open();
string r = @"update counter set m_id=m_id+1 where id=1;
select m_id from counter where id=1;";
SqlCommand com = new SqlCommand(r, sql);
int result = (int)com.ExecuteScalar();
sql.Close();
{
SendHTMLMail(result);
}
}
此时 SendHTMLMail 方法(嵌套或非嵌套)可以接收 result 变量并避免对数据库引擎的任何调用。请注意,在这种情况下,我已将 ExecuteNonQuery 更改为 ExecuteScalar 以取回 select.
返回的唯一值
但是,如果仍然希望将事物分开,那么您需要将 SendHTMLMail 中的代码更改为类似这样的代码
// just change the CommandText to avoid reexecuting the INSERT
com.CommandText = "select m_id from counter where id=1";
// No more needed
// SqlCommand ccmd = new SqlCommand(re, sql);
// Get the reader from the SqlCommand
SqlDataReader read = com.ExecuteReader();
{
.... go on with reading
我正在做的是单击一个按钮,我正在通过向其添加 +1 来更新 sql 列。这样做我没有遇到任何问题。现在我想要的是,对于同一个按钮单击事件,我想读取我已更新的那一列的值,并将其在查询字符串中发送到另一个页面。这是代码-
int i=0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
sql.Open();
string r = "update counter set m_id=m_id+1 where id=1";
SqlCommand com = new SqlCommand(r, sql);
com.ExecuteNonQuery();
sql.Close();
{
SendHTMLMail();
}
void SendHTMLMail()
{
StreamReader reader = new StreamReader(dd1.SelectedItem.Value);
string readFile = reader.ReadToEnd();
Regex regx = new Regex("(?<!src=\")http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([a-zA-Z0-9\?\#\=\/]){1})?", RegexOptions.IgnoreCase);
string output = regx.ToString();
string count = 0.ToString();
output = readFile;
string username = Server.UrlEncode(this.txtUsername.Text);
//here i want to read the value of m_id that i have updated as shown above and setting it to 'i' and passing this i to the query string//
sql.Open();
string re = "select m_id from counter where id=1";
SqlCommand ccmd = new SqlCommand(re, sql);
com.ExecuteNonQuery();
SqlDataReader read = ccmd.ExecuteReader();
{
while (read.Read())
i =int.Parse(read["m_id"].ToString());
}
sql.Close();
output = regx.Replace(output, new MatchEvaluator((match) =>
{
var url = Uri.EscapeDataString(match.Value.ToString());
return $"http://localhost:61187/two?sender={username}&link={url}&count={count}&mailer_id={i}";
}));
现在,问题不是从 5 更新到 6,而是 m_id 更新到 7
您可以批处理 命令,只执行一个命令而不是调用数据库两次
protected void btnSubmit_Click(object sender, EventArgs e)
{
sql.Open();
string r = @"update counter set m_id=m_id+1 where id=1;
select m_id from counter where id=1;";
SqlCommand com = new SqlCommand(r, sql);
int result = (int)com.ExecuteScalar();
sql.Close();
{
SendHTMLMail(result);
}
}
此时 SendHTMLMail 方法(嵌套或非嵌套)可以接收 result 变量并避免对数据库引擎的任何调用。请注意,在这种情况下,我已将 ExecuteNonQuery 更改为 ExecuteScalar 以取回 select.
返回的唯一值但是,如果仍然希望将事物分开,那么您需要将 SendHTMLMail 中的代码更改为类似这样的代码
// just change the CommandText to avoid reexecuting the INSERT
com.CommandText = "select m_id from counter where id=1";
// No more needed
// SqlCommand ccmd = new SqlCommand(re, sql);
// Get the reader from the SqlCommand
SqlDataReader read = com.ExecuteReader();
{
.... go on with reading