mySqlCommand.ExecuteNonQuery();得问题????

zsufrog 2004-07-11 12:26:16
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication3.example
{
/// <summary>
/// ex05_19 的摘要说明。
/// </summary>
public class ex05_19 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
protected System.Web.UI.WebControls.RangeValidator RangeValidator1;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.TextBox TextBox3;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection=new SqlConnection("server=localhost;uid=sa;pwd=tian0815;database=Aspnet");
string strSQL="INSERT INTO Ex0501 (用户名,密码,学号,电子邮件) VALUES ( '"+TextBox1.Text+"','"+TextBox2.Text+"', '"+TextBox3.Text+"', '"+TextBox4.Text+"', '"+TextBox5.Text+"')";

try
{
myConnection.Open();
SqlCommand mySqlCommand=new SqlCommand(strSQL,myConnection);
mySqlCommand.ExecuteNonQuery();//关键在这里出问题
Label1.Text="注册成功";
}
catch
{

Label1.Text="数据库连接失败!";

}
finally
{
myConnection.Close();
}
}


}



}

如果去掉mySqlCommand.ExecuteNonQuery();这个语句就不会出现“数据库连接失败”的提示,但是,去掉之后,虽然提示注册成功,但是察看数据库相对应得表,并没有看到有东西写入数据库(这个表仍然为空)
...全文
548 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2004-07-11
  • 打赏
  • 举报
回复
catch the exception to see the detailed error messages

try
{
myConnection.Open();
SqlCommand mySqlCommand=new SqlCommand(strSQL,myConnection);
mySqlCommand.ExecuteNonQuery();//关键在这里出问题
Label1.Text="注册成功";
}
catch (Exception ex)
{

Label1.Text= ex.Message;
if (ex.InnerException != null)
Label1.Text += ex.InnerException.Message;

}
finally
{
myConnection.Close();
}


also, use string concatenation to form SQL is susceptible to SQL injection attack, use Parameter approach, see

http://msdn.microsoft.com/library/en-us/dnnetsec/html/SecNetch12.asp?frame=true
jierry007 2004-07-11
  • 打赏
  • 举报
回复
ExecuteNonQuery()不能去掉,去掉了就不执行数据库操作了
loveeqing 2004-07-11
  • 打赏
  • 举报
回复
mySqlCommand.ExecuteNonQuery()这不是执行SQL语句的吗?怎么能够去掉
sunrongxa 2004-07-11
  • 打赏
  • 举报
回复
显示出错信息Label1.Text= ex.Message;
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;// using System.Collections;// /// /// SQLHelper 的摘要说明 /// public class SQLHelper { public SQLHelper() { // // TODO: 在此处添加构造函数逻辑 // } private static SqlCommand command = new SqlCommand();// private static SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); static SQLHelper() { command.Connection = conn; } /// /// 打开数据库连接 /// private static void OpenConnect() { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); } /// /// 关闭数据库连接 /// public static void CloseConnect() { if (conn.State != ConnectionState.Closed) { conn.Close(); } } /// /// /// /// /// /// private static SqlCommand CreateCommand(string procName, SqlParameter[] prams) { OpenConnect();//打开DB连接 SqlCommand command = new SqlCommand(procName, conn); command.CommandType = CommandType.StoredProcedure; if (prams != null) { foreach (SqlParameter parameter in prams) { command.Parameters.Add(parameter); } } command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; } /// /// 执行sql命令语句 /// /// 传入要执行的sql语句 public static void ExecuteSql(string sql) { OpenConnect();//打开连接 command.CommandType = CommandType.Text; command.CommandText = sql; command.ExecuteNonQuery(); CloseConnect();//关闭连接 } /// /// 返回一个DataTable对象 /// /// 要执行的SQL命令语句 /// 返回一的DataTable对象 public static DataTable GetDataTable(string sql) { DataSet dataSet = new DataSet(); OpenConnect();//打开DB连接 SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; command.CommandText = sql; command.CommandType = CommandType.Text; adapter.Fill(dataSet); CloseConnect();//关闭DB连接 return dataSet.Tables[0]; } /// /// 返回一的DataTable对象 /// /// 要执行的SQL命令语句 /// 第几页 /// 每页显示数据项数 /// /// 返回一的DataTable对象 public static DataTable GetDataTable(string sql, int pageinfo, int pagesize, string name) { DataSet dataSet = new DataSet(); OpenConnect(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; command.CommandText = sql; command.CommandType = CommandType.Text; adapter.Fill(dataSet, pageinfo, pagesize, name); CloseConnect(); return dataSet.Tables[0]; } /// /// 返回第一行的第一列的值 /// /// 要执行的SQL命令语句 /// 字符串形式返回查询结果第一行的第一列的值 public static string GetFirstColumnValue(string sql) { OpenConnect(); command.CommandType = CommandType.Text; command.CommandText = sql; object obj2 = command.ExecuteScalar(); CloseConnect(); if (obj2 == null) { return ""; } return obj2.ToString(); } /// /// 返回一个SqlDataReader对象 /// /// 要执行的SQL命令语句 /// public static SqlDataReader GetReader(string sql) { OpenConnect(); command.CommandType = CommandType.Text; command.CommandText = sql; return command.ExecuteReader(CommandBehavior.CloseConnection);//若关闭DataReader对象则关联的Connection对象也将关闭 } /// /// 返回一个bool值判断Reader对象是否存在行 /// /// 要执行的SQL命令语句 /// 返回一个bool值判断Reader对象是否存在行 public static bool IsHasRow(string sql) { OpenConnect(); command.CommandType = CommandType.Text; command.CommandText = sql; bool flag = command.ExecuteReader().Read(); CloseConnect(); return flag; } public static DataSet RunProc(string procName, SqlParameter[] prams) { DataSet dataSet = new DataSet(); OpenConnect(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = CreateCommand(procName, prams); adapter.Fill(dataSet); CloseConnect(); return dataSet; } public static void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader) { dataReader = CreateCommand(procName, prams).ExecuteReader(CommandBehavior.CloseConnection); } public static void RunProcNon(string procName, SqlParameter[] prams) { OpenConnect(); CreateCommand(procName, prams).ExecuteNonQuery(); CloseConnect(); } public static void RunProcNonQuery(string procName, SqlParameter[] prams) { CreateCommand(procName, prams).ExecuteNonQuery(); } public static DataTable RunProcT(string procName, SqlParameter[] prams) { DataSet dataSet = new DataSet(); OpenConnect(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = CreateCommand(procName, prams); adapter.Fill(dataSet); CloseConnect(); return dataSet.Tables[0]; } //-------------------------------------------------------------------------------------------------------------- public static void ExecuteSqlWithIdentityTrans(ArrayList sqls) { string newValue = null; string str2 = ""; SqlTransaction transaction = null; try { OpenConnect();//打开连接 transaction = conn.BeginTransaction();//开始数据库事务 command.Transaction = transaction; foreach (string str3 in sqls) { str2 = str3; if (newValue != null) { str2 = str3.Replace("IdenStr", newValue); } command.CommandType = CommandType.Text; command.CommandText = str2; if (str3.IndexOf("Idenity") != -1) { newValue = command.ExecuteScalar().ToString(); continue; } command.ExecuteNonQuery(); } transaction.Commit();//提交数据库事务 } catch (Exception exception) { transaction.Rollback();//回滚事务 throw exception; } finally { CloseConnect();//关闭连接 } } public static void ExecuteSqlWithTrans(ArrayList sqls) { SqlTransaction transaction = null; try { OpenConnect(); transaction = conn.BeginTransaction(); command.Transaction = transaction; foreach (string str in sqls) { command.CommandType = CommandType.Text; command.CommandText = str; command.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception exception) { transaction.Rollback(); throw exception; } finally { CloseConnect(); } } public static SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) { return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); } public static SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size) { return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null); } public static SqlParameter MakeParam(string ParamName, SqlDbType DbType, int Size, ParameterDirection Direction, object Value) { SqlParameter parameter; if (Size > 0) { parameter = new SqlParameter(ParamName, DbType, Size); } else { parameter = new SqlParameter(ParamName, DbType); } parameter.Direction = Direction; if ((Direction != ParameterDirection.Output) || (Value != null)) { parameter.Value = Value; } return parameter; } public static SqlParameter MakeReturnParam(string ParamName, SqlDbType DbType, int Size) { return MakeParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null); } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Data.SqlClient; using System.Text; using MySql.Data; using MySql.Data.MySqlClient; //本程序的功能是实时从mysql中读取一条记录中的一个字段,插入到sqlserver 中,经过长时间的运行,基本无错了, 而这数据同步,目前没有实时性强的软件,至少1分钟,太久了,自己写了一个, //可以自己设置同步时间,字符连接串等,给了源码,而且对程序进行了很多优化 容错性很强,懂点程序的 就可以直接修改源码 就可以用了。由于没有时间按做界面,只能自己修改了,不过已经很稳定了,为了给大家提供点方便,免得还去查大量的资料。 namespace tool { public partial class Form1 : Form { public string mysql_Conn = "Database='cmccbbs';Data Source='211.139.22.124';UserId='cmcc';Password='CMCC2014cmcc2014';charset='utf8';pooling=true"; public string mysql_sql = "select max(id) from pre_sms_tempsend"; // public string sql_server_Conn = "Data Source=lei; DataBase=test; User=sa;PWD=123456"; 211.222.229.124 public string id, phone, verify, last_id; SqlConnection my_sql_con; MySqlConnection con; MySqlCommand cmd; DataSet ds; public string sql_server_Conn = "Data Source=222.85.144.112,14444; DataBase=DB_CustomSMS; User=sms;PWD=sms2014"; // public string sql_sql = "select max(id) from pre_sms_tempsend"; MySqlDataAdapter md; public int ii = 0; public Form1() { InitializeComponent(); timer1.Interval = 1000; //连接mysql con = new MySql.Data.MySqlClient.MySqlConnection(mysql_Conn); con.Open(); cmd = new MySqlCommand("select * from pre_sms_tempsend order by id desc limit 1", con); cmd.Connection = con; md = new MySqlDataAdapter(); ds = new DataSet(); //这样做避免一直new,导致内存泄露 } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "start") { timer1.Enabled = true; button1.Text = "stop"; }

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧