首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何数据写不进数据库啊又没有报错

2012-02-07 
怎么数据写不进数据库啊,又没有报错usingSystemusingSystem.DatausingSystem.ConfigurationusingSystem

怎么数据写不进数据库啊,又没有报错
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;

public   partial   class   _Default   :   System.Web.UI.Page  
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {

             
        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {  
                     
                      //   myConnection.Open();
                if   (!Page.IsValid)
                {

                          string   conn   =   "server=741FC6DFB4214A2;database=pingou;user   id=sa;password=sa ";
                          SqlConnection   myConnection   =   new   SqlConnection(conn);
                          string   sql   =   "select   *   from   tbTree   where   ID   = '+this.TextBox1.Text+ ' ";
                          SqlCommand   myCommand   =   new   SqlCommand(sql,   myConnection);
                          myConnection.Open();
                          SqlDataReader     myDataReader   =   myCommand.ExecuteReader();

                    DataTable   dt=   new   DataTable();
                        dt.Columns.Add(new   DataColumn( "ID ",typeof(string)));
                        dt.Columns.Add(new   DataColumn( "Name ",typeof(string)));
                        dt.Columns.Add(new   DataColumn( "ParentID ",typeof(string)));


                        while(myDataReader.Read())
                        {
                                DataRow   dr=   dt.NewRow();
                                dr[0]=myDataReader.GetValue(0).ToString();
                                dr[1]=myDataReader.GetValue(1).ToString();
                                dr[2]=myDataReader.GetValue(2).ToString();


                        }
              myConnection.Close();
                        this.GridView1.DataSource   =   new   DataView(dt);
                        this.GridView1.DataBind();

              /*       try
                        {
                                myCommand.Connection.Open();
                                myCommand.ExecuteNonQuery();
                                Label2.Text   =   myCommand.ToString();

                             
                        }
                        catch
                        {
                                Label2.Text   =   "失败! ";
                        }*/
                }
        }
        protected   void   Button2_Click(object   sender,   EventArgs   e)
        {


                string   conn   =   "server=741FC6DFB4214A2;database=pingou;user   id=sa;password=sa ";
                SqlConnection   myConnection   =   new   SqlConnection(conn);
                string   sql   =   "inrt   into   tbTree   (Name)   values   (@cName) ";
                SqlCommand   addCommand   =   new   SqlCommand(sql,   myConnection);


             
           
                   
                        addCommand.CommandType   =   CommandType.StoredProcedure;

                        addCommand.Parameters.Add(new   SqlParameter( "@ID ",   SqlDbType.Int));
                        addCommand.Parameters.Add(new   SqlParameter( "@Name ",   SqlDbType.NVarChar,   50));
                        addCommand.Parameters.Add(new   SqlParameter( "@ParentID ",SqlDbType.Int));

                        addCommand.Parameters[ "@ID "].Value   =   this.tbID.Text;


                        addCommand.Parameters[ "@Name "].Value   =   this.tbName.Text;
                        addCommand.Parameters[ "@ParentID "].Value   =   this.tbPID.Text;
                        try
                        {
                                addCommand.Connection.Open();
                                addCommand.ExecuteNonQuery();
                                Label2.Text   =   "成功 ";


                        }
                        catch
                        {
                                Label2.Text   =   "失败! ";
                        }
        }
}


谢谢了

[解决办法]
string sql = "inrt into tbTree (Name) values (@cName) ";

inrt 改过来先

热点排行