数据库链接 字符信息
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SqlClient; 6 using System.Data; 7 8 namespace DALPublic 9 { 10 public class DALMethod 11 { 12 13 #region 连接字符串 14 15 16 public string strConnect = @"Data Source=DESKTOP-SDSDE;Initial Catalog=SJSSDDS;User ID=sa;Password=123"; 17 18 #endregion 19 20 #region 插入、更新、删除 21 public int UpdateData(String sql, SqlParameter[] param) 22 { 23 int count = 0; 24 25 using (SqlConnection conn = new SqlConnection(strConnect)) 26 { 27 conn.Open(); 28 SqlCommand cmd = new SqlCommand(sql, conn); 29 cmd.CommandType = CommandType.StoredProcedure; 30 cmd.Parameters.AddRange(param); 31 count = cmd.ExecuteNonQuery(); 32 conn.Close(); 33 } 34 35 return count; 36 } 37 #endregion 38 39 #region 查询数据表 40 public DataTable QueryDataTable(String sql, SqlParameter[] param) 41 { 42 DataTable dt = new DataTable(); 43 using (SqlConnection conn = new SqlConnection(strConnect)) 44 { 45 conn.Open(); 46 SqlDataAdapter da = new SqlDataAdapter(sql, conn); 47 da.SelectCommand.CommandType = CommandType.StoredProcedure; 48 da.SelectCommand.Parameters.AddRange(param); 49 da.Fill(dt); 50 conn.Close(); 51 } 52 53 return dt; 54 } 55 #endregion 56 57 #region 二进制文件查询方法 58 public byte[] QueryDataByte(String sql, SqlParameter[] param) 59 { 60 using (SqlConnection conn = new SqlConnection(strConnect)) 61 { 62 conn.Open();//打开连接 63 SqlDataReader dr = null;//初始化数据读取器 64 SqlCommand cmd = new SqlCommand(sql, conn);//创建命令对象 65 cmd.CommandType = CommandType.StoredProcedure;//设置命令类型 66 cmd.Parameters.AddRange(param);//把参数加进命令对象 67 dr = cmd.ExecuteReader();//启动dr 68 byte[] File = null;//初始化byte 69 if (dr.Read())//执行把二进制流加进byte[]中 70 { 71 File = (byte[])dr[0]; 72 } 73 dr.Close();//关闭数据读取器 74 conn.Close();//关闭连接 75 return File; 76 } 77 } 78 #endregion 79 80 } 81 }