读写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();
            }
        }
    }

 

posted on 2013-11-15 17:43  筑梦1582  阅读(204)  评论(0编辑  收藏  举报