欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

詳解C#中SqlParameter的作用與用法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

一般來說,在更新DataTable或是DataSet時,如果不采用SqlParameter,那么當輸入的Sql語句出現歧義時,如字符串中含有單引號,程序就會發(fā)生錯誤,并且他人可以輕易地通過拼接Sql語句來進行注入攻擊。

string sql
 = "update
 Table1 set name = 'Pudding' where ID = '1'";//未采用SqlParameter
SqlConnection
 conn = new SqlConnection();
conn.ConnectionString
 = "Data
 Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";//連接字符串與數據庫有關
SqlCommand
 cmd = new SqlCommand(sql,
 conn);
try
{
  conn.Open();
  return(cmd.ExecuteNonQuery());
}
catch (Exception)
{
  return -1;
  throw;
}
finally
{
  conn.Close();
}

上述代碼未采用SqlParameter,除了存在安全性問題,該方法還無法解決二進制流的更新,如圖片文件。通過使用SqlParameter可以解決上述問題,常見的使用方法有兩種,Add方法和AddRange方法。

一、Add方法

SqlParameter
 sp = new SqlParameter("@name","Pudding");
cmd.Parameters.Add(sp);
sp
 = new SqlParameter("@ID","1");
cmd.Parameters.Add(sp);

該方法每次只能添加一個SqlParameter。上述代碼的功能是將ID值等于1的字段name更新為Pudding(人名)。

二、AddRange方法

SqlParameter[]
 paras = new SqlParameter[]
 { new SqlParameter("@name","Pudding"),new SqlParameter("@ID","1")
 };
cmd.Parameters.AddRange(paras);

顯然,Add方法在添加多個SqlParameter時不方便,此時,可以采用AddRange方法。

下面是通過SqlParameter向數據庫存儲及讀取圖片的代碼。

public int SavePhoto(string photourl)
{
  FileStream
 fs = new FileStream(photourl,
 FileMode.Open, FileAccess.Read);//創(chuàng)建FileStream對象,用于向BinaryReader寫入字節(jié)數據流
  BinaryReader
 br = new BinaryReader(fs);//創(chuàng)建BinaryReader對象,用于寫入下面的byte數組
  byte[]
 photo = br.ReadBytes((int)fs.Length);//新建byte數組,寫入br中的數據
  br.Close();//記得要關閉br
  fs.Close();//還有fs
  string sql
 = "update
 Table1 set photo = @photo where ID = '0'";
  SqlConnection
 conn = new SqlConnection();
  conn.ConnectionString
 = "Data
 Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";
  SqlCommand
 cmd = new SqlCommand(sql,
 conn);
  SqlParameter
 sp = new SqlParameter("@photo",
 photo);
  cmd.Parameters.Add(sp);
  try
  {
    conn.Open();
    return (cmd.ExecuteNonQuery());
  }
  catch (Exception)
  {
    return -1;
    throw;
  }
  finally
  {
    conn.Close();
  }
}
 
public void ReadPhoto(string url)
  {
    string sql
 = "select
 photo from Table1 where ID = '0'";
    SqlConnection
 conn = new SqlConnection();
    conn.ConnectionString
 = "Data
 Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";
    SqlCommand
 cmd = new SqlCommand(sql,
 conn);
    try
    {
      conn.Open();
      SqlDataReader
 reader = cmd.ExecuteReader();//采用SqlDataReader的方法來讀取數據
      if (reader.Read())
      {
        byte[]
 photo = reader[0] as byte[];//將第0列的數據寫入byte數組
        FileStream
 fs = new FileStream(url,FileMode.CreateNew);創(chuàng)建FileStream對象,用于寫入字節(jié)數據流
        fs.Write(photo,0,photo.Length);//將byte數組中的數據寫入fs
        fs.Close();//關閉fs
      }
      reader.Close();//關閉reader
    }
    catch (Exception
 ex)
    {
      throw;
    }
    finally
    {
      conn.Close();
    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:DevExpress TreeList 常見問題解決方法

欄    目:C#教程

下一篇:C#與PHP的md5計算結果不同的解決方法

本文標題:詳解C#中SqlParameter的作用與用法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6052.html

網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

如果侵犯了您的權利,請與我們聯系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有