添加,删除,更新数据庫

本文介绍如何使用C#进行基本的SQL操作,包括更新数据、查询单值及调用存储过程的方法,并展示了如何利用ExecuteNonQuery进行数据修改的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


           1)如果不返回行,用以下语句:

private
 void button1_Click(object sender, EventArgs e)
        
{
            SqlConnection con 
= new SqlConnection();
            con.ConnectionString 
= (@"Data source=localhost;Integrated Security=true;database=Northwind");
            
try
            
{
                
string SQL = "UPDATE Categories SET CategoryName='" + textBox1.Text + "'" + " WHERE CategoryID='1'"
                SqlCommand cmd 
= new SqlCommand();
                cmd.CommandText 
= SQL;
                cmd.Connection 
= con; 
                con.Open();

                
int rowAffected = cmd.ExecuteNonQuery();
                
if (rowAffected == 1)
                    label1.Text 
= "Updated sucessuflly!";
                


            }

            
catch (SqlException es)
            
{
                MessageBox.Show(es.Message);
            }
 

             2)如果要返回单值(行)的查询,用以下语句:
            
                //cmd.ExecuteNonQuery();
               int intCustomerIDOrdinal = Convert.ToInt32(cmd.ExecuteScalar());//返回一个表示列序号的整数
               string strCategoryName = Convert.ToString(cmd.ExecuteScalar());//单值查询
               
            3)调用存储过程
 private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = (@"Data source=localhost;Integrated Security=true;database=Northwind");
            try
            {
            //    string SQL = "UPDATE Categories SET CategoryName='" + textBox1.Text + "'" + " WHERE CategoryID='1'";
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                cmd.CommandText = "GetCustomer";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5).ToString();
                cmd.Parameters[0].Value = "ALFKI";

                SqlDataReader reader = cmd.ExecuteReader();
                           


            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message);
            }
存储过程如下:
CREATE PROCEDURE GetCustomer(@CustomerID nChar(5))  
AS 
SELECT CustomerID,CompanyName,ContactName,ContactTitle 
FROM Customers WHERE CustomerID=@CustomerID 


          4)调用ExecuteNonQuery方法执行储存在Command中的查询:
示例将订单细节提取到一个DataTable中,修改订单内容,再向数据提交更改。
private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection();
            string sql = "SELECT * FROM Employees";
            con.ConnectionString = @"Data source=localhost;Integrated Security=true;database=Northwind";
            try
            {
                con.Open();
                DataTable tb1;
                tb1=GetTable();//初始化表
                ResetOrder();//更新表(插入,删除)
                SqlDataAdapter ad = new SqlDataAdapter(sql,con);
                ad.Fill(tb1);
                
              
             }          
            catch (SqlException es)
            {
                MessageBox.Show(es.Message);
            }
        }

     
        static DataTable GetTable()
        {
            DataTable tb1 = new DataTable("Employees");
            DataColumn col;
            col = tb1.Columns.Add("EmployeeID", typeof(int));
            col.AllowDBNull = false;
            col = tb1.Columns.Add("LastName",typeof(string));
            col.AllowDBNull = false;   
            tb1.PrimaryKey = new DataColumn[] { tb1.Columns["OrderID"], tb1.Columns["ProductID"] };
            return tb1;
        }
        static void ResetOrder()
        {  
            string strSQL;
          
            SqlConnection con = new SqlConnection();
            con.ConnectionString = (@"Data source=localhost;Integrated Security=true;database=Northwind");
            con.Open();
            SqlCommand cmd = con.CreateCommand();
               //插入一行
            strSQL = "INSERT INTO Employees(FirstName,LastName,Notes) VALUES('firstname','lastname','notes')";
            cmd.CommandText=strSQL;
            cmd.ExecuteNonQuery();
              //删除一行
            strSQL="DELETE FROM Employees WHERE EmployeeID='112237'";
            cmd.CommandText = strSQL;
            cmd.ExecuteNonQuery();
                    
        }
    }
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值