zoukankan      html  css  js  c++  java
  • ado.net 插入一条数据

     /// <summary>
            //插入数据
            /// </summary>
            /// <param name="SQLString">计算查询结果语句</param>
            /// <returns>查询结果(object)</returns>
            public  int  ExecuteData(string SQLString, params SqlParameter[] cmdParms)
            {
                string connectionString = "Data Source=192.168.42.97;Initial Catalog=DataCompare;User ID=sa;Password=sa";
                int ret = 0;
                try
                {
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.Parameters.AddRange(cmdParms);
                            cmd.CommandText = SQLString;
                            ret = cmd.ExecuteNonQuery();
                        }
     
                    }
                }
                catch (Exception ex)
                {
                    ret = 0;
                
                }
                return ret;
            }
     
     
     
      /// <summary>
            /// 增加一条数据
            /// </summary>
            public int Add(InputData model)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into InputData(");
                strSql.Append("code,name,newpric,jys,mtime,datatype,createtime");
                strSql.Append(") values (");
                strSql.Append("@code,@name,@newpric,@jys,@mtime,@datatype,@createtime");
                strSql.Append(") ");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
                   new SqlParameter("@code", SqlDbType.NVarChar,100) ,            
                            new SqlParameter("@name", SqlDbType.NVarChar,100) ,            
                            new SqlParameter("@newpric", SqlDbType.NVarChar,20) ,            
                            new SqlParameter("@jys", SqlDbType.NVarChar,100) ,            
                            new SqlParameter("@mtime", SqlDbType.DateTime) ,            
                            new SqlParameter("@datatype", SqlDbType.NChar,10) ,            
                            new SqlParameter("@createtime", SqlDbType.DateTime)             
                  
                };
                parameters[0].Value = model.code;
                parameters[1].Value = model.name;
                parameters[2].Value = model.newpric;
                parameters[3].Value = model.jys;
                parameters[4].Value = model.mtime;
                parameters[5].Value = model.datatype;
                parameters[6].Value = model.createtime;
                object obj = ExecuteData(strSql.ToString(), parameters);
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
  • 相关阅读:
    BeforeFieldInit解析(zz)
    自定义控件
    How to make Office VBA code interact with a VSTO applicationlevel addin
    总结
    IL汇编语言程序设计目录
    zz如何精简用户界面
    volatile关键字的作用(zz)
    外企面试基本涵盖了所有问题(拿得别人的,很有用)
    Excel 2007 There was a problem sending the command to the program
    数据库技巧
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628969.html
Copyright © 2011-2022 走看看