C# 后端往数据库插入、更新、删除记录

   public void Insert(int UserID, string UserName, string PassWord, string Tel)
        {
            var _adapter = new System.Data.SqlClient.SqlDataAdapter();             
            var InsertCommand = new System.Data.SqlClient.SqlCommand();
            var _connection = new System.Data.SqlClient.SqlConnection();
            _connection.ConnectionString = WebApplication4.Properties.
                Settings.Default.db_sql20001ConnectionString;

            InsertCommand.Connection = _connection;
            InsertCommand.CommandText = "INSERT INTO [dbo].[tb_UserLogin] ([UserID], [UserName], [PassWord], [Tel]) VALUES" +
                " (@UserID, @UserName, @PassWord, @Tel)";
            InsertCommand.CommandType = System.Data.CommandType.Text;
            InsertCommand.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Input, 0, 0, "UserID", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            InsertCommand.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "UserName", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            InsertCommand.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@PassWord", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "PassWord", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            InsertCommand.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@Tel", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "Tel", System.Data.DataRowVersion.Current, false, null, "", "", ""));

            InsertCommand.Parameters[0].Value = ((int)(UserID));
            if ((UserName == null))
            {
                InsertCommand.Parameters[1].Value = System.DBNull.Value;
            }
            else
            {
                InsertCommand.Parameters[1].Value = ((string)(UserName));
            }
            if ((PassWord == null))
            {
                InsertCommand.Parameters[2].Value = System.DBNull.Value;
            }
            else
            {
                InsertCommand.Parameters[2].Value = ((string)(PassWord));
            }
            if ((Tel == null))
            {
                InsertCommand.Parameters[3].Value = System.DBNull.Value;
            }
            else
            {
                InsertCommand.Parameters[3].Value = ((string)(Tel));
            }
            System.Data.ConnectionState previousConnectionState = InsertCommand.Connection.State;
            if (((InsertCommand.Connection.State & System.Data.ConnectionState.Open)
                        != System.Data.ConnectionState.Open))
            {
                InsertCommand.Connection.Open();
            }
            try
            {
                int returnValue = InsertCommand.ExecuteNonQuery();

            }
            finally
            {
                if ((previousConnectionState == System.Data.ConnectionState.Closed))
                {
                    InsertCommand.Connection.Close();
                }
            }
        }


        public void Insert2(int UserID, string UserName, string PassWord, string Tel)
        {            
            var _Command = new System.Data.SqlClient.SqlCommand();
            var _connection = new System.Data.SqlClient.SqlConnection();
            _connection.ConnectionString = WebApplication4.Properties.
                Settings.Default.db_sql20001ConnectionString;

            _Command.Connection = _connection;

//更新和删除跟增加类似,修改这里就好
            _Command.CommandText = "INSERT INTO [dbo].[tb_UserLogin] ([UserID], [UserName], [PassWord], [Tel]) VALUES" +
                " (@UserID, @UserName, @PassWord, @Tel)";
            _Command.CommandType = System.Data.CommandType.Text;
            _Command.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Input, 0, 0, "UserID", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            _Command.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "UserName", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            _Command.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@PassWord", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "PassWord", System.Data.DataRowVersion.Current, false, null, "", "", ""));
            _Command.Parameters.Add(
                new System.Data.SqlClient.SqlParameter("@Tel", System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0, 0, "Tel", System.Data.DataRowVersion.Current, false, null, "", "", ""));

            _Command.Parameters[0].Value = ((int)(UserID));
            if ((UserName == null))
            {
                _Command.Parameters[1].Value = System.DBNull.Value;
            }
            else
            {
                _Command.Parameters[1].Value = ((string)(UserName));
            }
            if ((PassWord == null))
            {
                _Command.Parameters[2].Value = System.DBNull.Value;
            }
            else
            {
                _Command.Parameters[2].Value = ((string)(PassWord));
            }
            if ((Tel == null))
            {
                _Command.Parameters[3].Value = System.DBNull.Value;
            }
            else
            {
                _Command.Parameters[3].Value = ((string)(Tel));
            }
            System.Data.ConnectionState previousConnectionState = _Command.Connection.State;
            if (((_Command.Connection.State & System.Data.ConnectionState.Open)
                        != System.Data.ConnectionState.Open))
            {
                _Command.Connection.Open();
            }
            try
            {
                int returnValue = _Command.ExecuteNonQuery();
            }
            finally
            {
                if ((previousConnectionState == System.Data.ConnectionState.Closed))
                {
                    _Command.Connection.Close();
                }
            }
        }
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值