/// <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); } }
ado.net 插入一条数据
最新推荐文章于 2022-01-08 12:29:16 发布