三個通用代碼

來源:互聯網
上載者:User
通用代碼 public SqlConnection connection_database(string server,string database,string userid,string password)
{
string str_con;
str_con="server="+server+";"+"database="+database+";"+"user id="+userid+";"+"

password="+password;
try
{
SqlConnection sqlcon=new SqlConnection (str_con);
sqlcon.Open();

return sqlcon;
}
catch
{
MessageBox.Show("資料庫連接不成功!請正確填寫好資料庫設定","錯誤",MessageBoxButtons.

OK,MessageBoxIcon.Stop);
Application.Exit();//應用程式退出
return null;
}

}
public DataView DvList(string logString,string tablename) //返回相應表的視圖.
{
if( sqlconn.State == ConnectionState.Closed)
sqlconn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(logString, sqlconn);
DataSet ds = new DataSet();
try
{
myCommand.Fill(ds, "+tablename+");
}
catch
{
MessageBox.Show("error");
}
DataView Dv = new DataView();
Dv = ds.Tables["+tablename+"].DefaultView;
return Dv;
}

public void Execute_SQL(string sql,SqlConnection sqlconn)//通用執行SQL函數
{
// 開啟資料庫連接
if( sqlconn.State == ConnectionState.Open)
sqlconn.Close();
sqlconn.Open();
SqlCommand cmd = new SqlCommand(sql, sqlconn);
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException ae)
{
MessageBox.Show(ae.Message.ToString());
}
cmd.Dispose();
}
public void show_listview_data(string select_str,string tablename,ListView lv)//通用的返回

樹視圖函數
{
//lv=new ListView();
lv.Clear();
this.sqlDataAdapter1=new SqlDataAdapter(select_str,this.sqlconn);
sqlDataAdapter1.Fill(dataSet21,"+tablename+");
//添加列名
for(int i=0;i<dataSet21.Tables["+tablename+"].Columns.Count;i++)
{
lv.Columns.Add(dataSet21.Tables["+tablename+"].Columns[i].ColumnName.

ToString(),90,HorizontalAlignment.Left);

}
//添加行(資料庫資料)
for(int j=0;j<dataSet21.Tables["+tablename+"].Rows.Count;j++)
{
ListViewItem item=lv.Items.Add(dataSet21.Tables["+tablename+"].Rows[j][0].

ToString());
item.Text=dataSet21.Tables["+tablename+"].Rows[j][0].ToString();
for(int i=1;i<dataSet21.Tables["+tablename+"].Columns.Count;i++)
item.SubItems.Add(dataSet21.Tables["+tablename+"].Rows[j][i].

ToString());
}
lv.View=View.Details;


}

}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。