iT邦幫忙

1

C# SqlConnection 常用SQLServer存取

  • 分享至 

  • xImage
  •  
    public DataSet GetDataSet(string sconn, string cmd)
    {
        try
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            using (SqlConnection sqlConn = new SqlConnection(sconn))
            {
                sqlConn.Open();
                SqlCommand sqlComm = new SqlCommand();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandTimeout = 3000;
                sqlComm.CommandText = cmd;

                SqlDataAdapter adapter = new SqlDataAdapter(sqlComm);                    
			    adapter.Fill(ds,"temp");                    
                return ds;
            }
        }
        catch (SqlException e)
        { 
            return null;
        }
    }
    public DataTable GetDataTable(string sconn, string cmd)
    {
        try
        {
            DataTable dt = new DataTable();
            using (SqlConnection sqlConn = new SqlConnection(sconn))
            {
                sqlConn.Open();
                SqlCommand sqlComm = new SqlCommand();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandTimeout = 3000;
                sqlComm.CommandText = cmd;

                SqlDataAdapter adapter = new SqlDataAdapter(sqlComm);
                adapter.Fill(dt);
                return dt;
            }
        }
        catch (SqlException e)
        { 
            return null;
        }
    }
    public string GetScalar(string sconn, string cmd)
    {
        try
        {
            object obj;
            using (SqlConnection sqlConn = new SqlConnection(sconn))
            {
                sqlConn.Open();
                SqlCommand sqlComm = new SqlCommand();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandTimeout = 3000;
                sqlComm.CommandText = cmd;
                obj = sqlComm.ExecuteScalar();
                if (obj == null)
                    obj = "";
                return obj.ToString();

            }
        }
        catch (SqlException e)
        { 
            return "";
        }
    }

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言