• 读写sqlserver数据库


    class SqlServerDataBase:IDataBase
        {
    
            String connstring = null;
    
            //执行没有返回值的SQL语句,如insert,update,delete
            public int executeSql_NoReturn(string strcmd)
            {
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand oprating = new SqlCommand(strcmd, conn);
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
    
                    object obj = oprating.ExecuteNonQuery();
                    return 0;
                }
                catch
                {
                    //MessageBox.Show("SQL数据格式错误",title,MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return -1;
                }
                finally
                {
                    conn.Close();
                }
            }
    
            //返回查询所返回的结果集中第一行的第一列或空引用(如果结果集为空)
            public string executeSql_GetCell(string strcmd)
            {
                SqlConnection conn =new  SqlConnection(connstring);
                SqlCommand oprating = new SqlCommand(strcmd, conn);
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    
                    object obj = oprating.ExecuteScalar();
                    if (obj != null)
                        return obj.ToString();
                    else
                        return null;
                }
                catch
                {
                    //MessageBox.Show("SQL数据格式错误",title,MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return "DataFormattingError";
                }
                finally
                {
                    conn.Close();
                }
            }
            //绑定数据源,用于输出数据集
            public DataTable executeSql_GetDataSet(string strcmd)
            {
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand oprating = new SqlCommand(strcmd, conn);
                SqlDataReader myDataReader = null;
                DataTable table = new DataTable();
    
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
    
                    myDataReader = oprating.ExecuteReader();
                    table.Load(myDataReader);
                    return table;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    conn.Close();
                }
             }
            public int ImagOper(string strcmd,byte[] Photograph,byte[] FingerPrint0,byte[] FingerPrint1)
            {
                StringBuilder strSql = new StringBuilder();
                SqlConnection conn = new SqlConnection(connstring);
                strSql.Append(strcmd);
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
    
                    SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
                    if (Photograph != null)
                        cmd.Parameters.Add("@Photograph", SqlDbType.Binary).Value = Photograph;
    
                    if (FingerPrint0 != null)
                        cmd.Parameters.Add("@FingerPrint0", SqlDbType.Binary).Value = FingerPrint0;
                    else
                        cmd.Parameters.Add("@FingerPrint0", SqlDbType.Binary).Value = new byte[1024];
    
                    if (FingerPrint1 != null)
                        cmd.Parameters.Add("@FingerPrint1", SqlDbType.Binary).Value = FingerPrint1;
                    else
                        cmd.Parameters.Add("@FingerPrint1", SqlDbType.Binary).Value = new byte[1024];
    
                    cmd.ExecuteNonQuery();
                    return 0;
                }
                catch(Exception e)
                {
                    if( e is SqlException)
                        return -3;
                    else
                        return -1;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
  • 相关阅读:
    动手动脑篇之类与对象
    团队精神
    在快乐中学习
    实习报告
    大道至简读后感(二)
    大道至简读后感
    读《大道至简》第一章有感
    指令随笔之:tail、cat、scp、&、&&、;、|、>、>>
    NFS安装过程
    CentOS7编译安装Nginx-1.8.1和编译参数
  • 原文地址:https://www.cnblogs.com/zhumeng1582/p/3425845.html
Copyright © 2020-2023  润新知