溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ADO.NET對(duì)SQL Server數(shù)據(jù)庫(kù)執(zhí)行增刪改查操作的示例分析

發(fā)布時(shí)間:2021-09-15 18:08:20 來(lái)源:億速云 閱讀:126 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)ADO.NET對(duì)SQL Server數(shù)據(jù)庫(kù)執(zhí)行增刪改查操作的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

先在自定義類的頭部定義好數(shù)據(jù)庫(kù)連接對(duì)象和連接字符串:

 string connectionString = "Data Source=SC-201607131829;Initial Catalog=Animal;Integrated Security=True";
  SqlConnection conn;

1.數(shù)據(jù)庫(kù)的查詢操作,返回一個(gè)DataTable

 public DataTable doSelect()
    {

      string sql = "select * from detial";

      using (conn = new SqlConnection(connectionString))
      {

        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter(sql, conn);

        DataSet ds = new DataSet();

        da.Fill(ds);  //填充DataSet

        return ds.Tables[0];

      }
    }

2.數(shù)據(jù)庫(kù)插入操作,返回布爾值

public bool doInsert(string name, string skin, string weight)
    {

      string sql = "insert into detial(name,skin,weight)values(@name,@skin,@weight)";

      SqlParameter[] newAnimal = {
         new SqlParameter("name",name),
         new SqlParameter("skin",skin),
         new SqlParameter("weight",skin)
      };

      using (conn = new SqlConnection(connectionString))
      {
        SqlCommand com = new SqlCommand(sql, conn);
        try
        {
          if (newAnimal != null)
          {
            foreach (SqlParameter parameter in newAnimal)
            {
              com.Parameters.Add(parameter);

            }
          }
          conn.Open();

          int influence = com.ExecuteNonQuery();

          if (influence > 0)
          {

            return true;
          }
          else
          {

            return false;
          }
        }
        catch (Exception exception)
        {
          return false;
        }
      }
    }

3.數(shù)據(jù)庫(kù)刪除操作,返回布爾值

public bool doDelete(string name)
    {

      string sql = "delete from detial where name = @name";

      SqlParameter[] deleteParameter = { new SqlParameter("name", name) };

      using (conn = new SqlConnection(connectionString))
      {

        SqlCommand com = new SqlCommand(sql, conn);
        
        try
        {

          if (deleteParameter != null)
          {
            foreach (SqlParameter parameter in deleteParameter)
            {
              com.Parameters.Add(parameter);
            }
            
          }

          conn.Open();

          int influence = com.ExecuteNonQuery();

          if (influence > 0)
          {

            return true;
          }
          else
          {

            return false;
          }
        }
        catch (Exception exception)
        {
          return false;
        }
      }
    }

4.數(shù)據(jù)庫(kù)更新操作,返回布爾值

public bool doUpdate(string name , string skin) {

      string sql = "update detial set skin = @skin where name = @name";
      SqlParameter[] updateParameter = {
                    new SqlParameter("name",name),
                    new SqlParameter("skin",skin)
      };

      using (conn = new SqlConnection(connectionString)) {

        SqlCommand com = new SqlCommand(sql,conn);

          try {

            if (updateParameter != null) { 
              
              foreach(SqlParameter parameter in updateParameter){

                com.Parameters.Add(parameter);

              } 
            }

            conn.Open();

            int influence = com.ExecuteNonQuery();

            if (influence > 0)
            {

              return true;
            }
            else
            {

              return false;
            }
          
          }catch(Exception exception){

            return false;
          }
      }

    }

其中為了防止sql注入,用到了SqlParameter類。

感謝各位的閱讀!關(guān)于“ADO.NET對(duì)SQL Server數(shù)據(jù)庫(kù)執(zhí)行增刪改查操作的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI