SqlParameter 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | SqlParameter string strSql = "Insert into News(TypeId,NewsCaption,NewsContent) values(@TypeId,@NewsCaption,@NewsContent)" ; SqlParameter[] paras ={ new SqlParameter( "@TypeId" ,SqlDbType.Int), new SqlParameter( "@NewsCaption" ,SqlDbType.NVarChar,200), new SqlParameter( "@NewsContent" ,SqlDbType.NText) }; paras[0].Value = typeId; paras[1].Value = caption; paras[2].Value = content; int rows = new SqlHelp().ExecuteNonQuery(strSql, paras); public SqlHelp() { connString = ConfigurationManager.AppSettings[ "sqlServer2005DbName" ]; } public int ExecuteNonQuery( string strQuery, SqlParameter[] paras) { int rows = 0; //影响行数 try { using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand command = new SqlCommand()) { PrepareCommand(conn, command, strQuery, paras); rows = command.ExecuteNonQuery(); return rows; } } } catch { throw ; } } //带参数的DML操作 private void PrepareCommand(SqlConnection conn, SqlCommand command, string strQuery, SqlParameter[] paras) { if (conn.State != ConnectionState.Open) { conn.Open(); } command.Connection = conn; command.CommandText = strQuery; command.CommandType = CommandType.Text; if (paras != null ) { foreach (SqlParameter parm in paras) command.Parameters.Add(parm); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步