web新增一条数据到数据库怎样实现?
用什么按件尼?
代杩怎样写?

解决方案 »

  1.   

    条件?拼接string sql="insert into myTable(name) values('"+textbox1.Text+"')";执行。ExecuteNonQuery
      

  2.   

    没有数据保存为插入新数据>insert
    存在的数据即为更新>update
    多条数据也一样>逐条inser或者update;
      

  3.   

    给你一个帮助文件,需要使用的时候直接调用,最后面是调用方法:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Configuration;
    using System.Data.SqlClient;
    using VtechDAL;
    //using DBUtility;namespace MyDAL
    {
        public class DBHelper
        {
                   //string strAppConfigFile = ConfigurationManager.AppSettings["DataBaseType"].ToString();        //Helper _helper = new Helper();
            //public  Helper GetHelper()
            //{            
            //    switch (strAppConfigFile)
            //    {
            //        case "System.Data.OracleClient":
            //            _helper.DatabaseType = Helper.DatabaseTypes.Oracle;
            //            break;
            //        case "MySql.Data.MySqlClient":
            //            _helper.DatabaseType = Helper.DatabaseTypes.MySql;
            //            break;
            //        case "System.Data.OleDb":
            //            _helper.DatabaseType = Helper.DatabaseTypes.OleDb;
            //            break;
            //        case "System.Data.SqlClient":
            //        default:
            //            _helper.DatabaseType = Helper.DatabaseTypes.Sql;
            //            break;
            //    }
            //    return _helper;        //}
                    private static SqlConnection connection;
            public static SqlConnection Connection
            {
                get
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["WEBDB"].ConnectionString;
                    if (connection == null)
                    {
                        connection = new SqlConnection(connectionString);
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }
            
            public int ExecuteCommand(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                int result = cmd.ExecuteNonQuery();
                return result;
            }        public int ExecuteCommand(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }        
            public int ExecuteCommand2(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }        public string GetScalar(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                string result = cmd.ExecuteScalar().ToString();
                return result;
            }        public string GetScalar(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                string result = cmd.ExecuteScalar().ToString();
                return result;
            }        public SqlDataReader GetReader(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }        public SqlDataReader GetReader(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }        public DataTable GetDataSet(string safeSql)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }        public DataTable GetDataSet(string sql, params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }        /// <summary>
            /// 返回数据集,从里面取得不同的表 
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="values"></param>
            /// <returns></returns>
            public DataSet  GetDataSet3(string sql, params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds;
            }
        }
    }
    调用的时候:
      /// <summary>
            /// New Info 
            /// </summary>
            /// <param name="pm"></param>
            /// <returns></returns>
            public bool UserInputPhilipInfo(PhilipModel pm)
            {
                string strSql = "CMBC.DBO.usp_UserInputPhilipInfo ";
                SqlParameter[] sp = new SqlParameter[] 
                {
                   new SqlParameter("@SoNo",pm.SoNo1),
                   new SqlParameter("@SoLine",pm.SoLine1),
                   new SqlParameter("@PoNo",pm.PoNo1),
                   new SqlParameter("@PoLine",pm.PoLine1),
                   new SqlParameter("@FactoryDate",pm.FactoryDate1),
                   new SqlParameter("@ShipPed",pm.ShipPed1),
                   new SqlParameter("@PMT_Re",pm.PMT_Re1),
                   new SqlParameter("@PlannerDate",pm.PlannerDate1),
                   new SqlParameter("@SoQty",pm.SoQty1),
                   new SqlParameter("@StausPMC",pm.StausPMC1),
                   new SqlParameter("@PlannerLatest",pm.PlannerLatest1),
                   new SqlParameter("@Re",pm.Re1),
                   new SqlParameter("@VtechPN",pm.VtechPN1),
                };
                return base.ExecuteCommand(strSql, sp) > 0 ? true : false;
            }