当前位置: 代码迷 >> ASP.NET >> cmd.ExecuteNonQuery()返回0 数据插入不成功 急 求各位大大看看,该如何解决
  详细解决方案

cmd.ExecuteNonQuery()返回0 数据插入不成功 急 求各位大大看看,该如何解决

热度:6800   发布时间:2013-02-25 00:00:00.0
cmd.ExecuteNonQuery()返回0 数据插入不成功 急急急 求各位大大看看
//数据操作的公共类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace DAL
{
  public class SqlAccess
  {


  private static string connStr = @"server=CHINA-6F2289564;database=DiagSys;uid=sa;pwd=;";
  //private static string connStr = @"server=.;database=DissSys;uid=sa;pwd=xingjian;";
  //private static string connStr = @"server=My-PC\SQL2005;database=DissSys;uid=sa;pwd=xingjian;";



  private static SqlConnection conn = new SqlConnection(connStr);
  //获取联接对象
  public static SqlConnection GetConn()
  {
   
  if (conn.State == ConnectionState.Closed)
  {
  conn.Open();
  }
  return conn;
   
  }

  public static void CloseCon()
  {
  conn.Close();
  }

  //获取查询结果记录集的dataSet对象
  public static DataSet GetDataSet(string sqlStr)//,string tableName)
  {
  //SqlCommand cmd=new SqlCommand(sqlStr,GetConn());
  SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, GetConn());
  DataSet ds = new DataSet();
  adapter.Fill(ds);
  return ds;
  }

  public static DataSet GetDataSet(string sqlStr,string tableName)
  {
  //SqlCommand cmd = new SqlCommand(sqlStr, GetConn());
  SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, GetConn());
  DataSet ds = new DataSet();
  adapter.Fill(ds,tableName);
  return ds;
  }

  //插入记录
  public static int ExecteNonQuery(string sqlStr)
  {
  int res = 0;
  SqlCommand cmd = new SqlCommand(sqlStr, GetConn());

  res = cmd.ExecuteNonQuery();
  try
  {
  res = cmd.ExecuteNonQuery();
  }
  catch
  {
  res = 0;
  }
  CloseCon();
  return res;
  }
  }
}




//数据操作的页面(执行操作)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 DAL;

public partial class Admin_dissNameAddBtn : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  /*病名添加按钮后台处理*/

  //string dissName = Request.QueryString["dissName"];
  string dissName = "肺炎";
  //string dissName = Server.UrlDecode(Request.QueryString["dissName"]).ToString().Replace(")", "").Replace("--", "").Replace(",", "");
  string sqlStr = "insert into dissInfo(diss_name)values('" + dissName + "')";
  int res = 0;
  try
  {



  res = SqlAccess.ExecteNonQuery(sqlStr);
  相关解决方案