當前位置:首頁 » 數據倉庫 » winform連接資料庫類
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

winform連接資料庫類

發布時間: 2022-09-26 16:52:57

① winform如何用代碼連接sqlite資料庫

最簡單的一種就是

string connectionString = "Data Source= D://test.db " ;

或者是:

<addname="sqlite"connectionString="DataSource=|DataDirectory|db;Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite"/>

其中 |DataDirectory|,這個是內置的.net目錄,可以替代運行時刻的當前目錄

使用例子:

using(DbConnectionconn=newSQLiteConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["sqlite"].ConnectionString))
{
conn.Open();
DbCommandcomm=conn.CreateCommand();
comm.CommandText="select*fromcustomer";
comm.CommandType=CommandType.Text;
using(IDataReaderreader=comm.ExecuteReader())
{
while(reader.Read())
{
Response.Write(reader[0]);
}
}
}

或者是:

stringconnectionString="DataSource="+AppDomain.CurrentDomain.BaseDirectory+"Data;Password=XXX";


基本連接Sqlite資料庫:

Data Source=mydb.db;Version=3;

--"Version" 的可能值: "2″ 指 SQLite 2.x (default);"3″ 指 SQLite 3.x
連接同時創建一個新的Sqlite資料庫:
Data Source=mydb.db;Version=3;New=True;
啟用壓縮連接Sqlite資料庫:
Data Source=mydb.db;Version=3;Compress=True;
指定連接Sqlite資料庫的緩存大小:
Data Source=mydb.db;Version=3;Cache Size=3000;

參考:

http://www.cnblogs.com/zhangji/archive/2013/03/04/2942736.html

② c# winform與資料庫的連接

在定義一個靜態的DBHelper類用於資料庫連接,
時直接類名點對象名就可以了。
public static string connString="Data Sourse=.;Initial CataLog=資料庫名;User Id=資料庫登陸名; Pwd=資料庫登陸密碼";
public static SqlConnection=new SqlConnection(connString);

③ winform如何連接資料庫

如需連接到本地SQL,請確保SQL服務正常啟動,然後執行如下代碼:
string con, sql;
/*Integrated Security=SSPI 這個表示以當前WINDOWS系統用戶身去登錄SQL SERVER伺服器,如果SQL SERVER伺服器不支持這種方式登錄時,就會出錯。
你可以使用SQL SERVER的用戶名和密碼進行登錄,如:
"Server=.;Database=YouDBName;User ID=sa;Password=密碼"
*/
con = "Server=.;Database=YouDBName;Integrated Security=SSPI";
sql = "select * from Test";
SqlConnection mycon = new SqlConnection(con);
mycon.Open();
SqlDataAdapter myda = new SqlDataAdapter(sql, con);
DataSet myds = new DataSet();
myda.Fill(myds, "Test");
dataGridView1.DataSource = myds.Tables["Test"];

④ winform怎樣連接資料庫

lianglin999(重樓)等級:#9樓 得分:8回復於:2010-03-28 21:25:32 使用SqlConnection類連接資料庫

//需要添加如下的命名空間
using System.Data.SqlClient;

protected void Button1_Click(object sender, EventArgs e)
{
//ConnectionString定義了連接字元串
string ConnectionString = "Data Source=. ; Initial Catalog=Pubs; User ID=sa";
//使用連接字元串構造一個SqlConnection實例
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
//打開連接
conn.Open();
//如果當前狀態打開,在控制台輸出
if (conn.State == ConnectionState.Open)
{
Label1.Text = "當前資料庫已經連接!<br/>";
Label1.Text += "連接字元串為:" + conn.ConnectionString;
}
}
catch (SqlException ex)
{
Label1.Text = "當前資料庫已經失敗!<br/>";
Label1.Text += "失敗的原因是:" + ex.Message;
}
finally
{
//調用Close方法即使關閉連接
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}

⑤ c# winform與資料庫的連接

在定義一個靜態的DBHelper類用於資料庫連接,
時直接類名點對象名就可以了。
public
static
string
connString="Data
Sourse=.;Initial
CataLog=資料庫名;User
Id=資料庫登陸名;
Pwd=資料庫登陸密碼";
public
static
SqlConnection
=new
SqlConnection(connString);

⑥ 求C#winform 資料庫類(連接和增刪改查)和在窗口中的調用代碼

C# 中分為三層,在數據訪問層中寫增刪查改的代碼和專門連接資料庫的一個類寫Connection;在窗口中調用的代碼一般寫在表示層。

⑦ winForm怎麼把連接資料庫的方法寫在類裡面

你可以寫一個通過類,代碼如下:
public Class DBHepler
{
public static DataSet GetDataSet(string strWhere)
{
public static string connString="Data Sourse=Localhost;Initial CataLog=資料庫名;User Id=資料庫登陸名; Pwd=資料庫登陸密碼";

SqlConnection sqlCon = new SqlConnection(connString);
SqlCommand sqlCom = new SqlCommand(sqlCom);
SqlCommand.CommandText = strWhere;
DataSet ds = new DataSet();

try
{
sqlCon.open();
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
sqlDA.Fill(ds);

return ds;
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
}
}
==================================================
然後就可以在你的方法中調用了:
private void getrf()
{
{
string str = "select 圖書編號,圖書名稱,出版社,圖書價格, 借閱次數,存放位置,庫存數量 from bookMessage order by 圖書編號 ASC";
DataSet ds = DBHepler.GetDataSet(str);
dataGridView1.DataSource = ds.Table[0].defaultView;
dataGridView1.DataBind();
}
}

⑧ 怎麼用VS的C#Winform連接sql資料庫

C#
連接資料庫的方式
WinForm和WebForm是一樣的
conn=new SqlConnection("server=資料庫伺服器地址;database=資料庫名稱;uid=sa;pwd=密碼;");//添加、修改、刪除 更換sql即可string sql="insert into tablename(欄位1,欄位2)values(值1,值2)";SqlCommand cmd=new SqlCommand(sql,conn);conn.Open();cmd.ExecuteNonQuery();conn.Close();//查詢sql="select * from tablename";SqlDataAdapter da=new SqlDataAdapter(sql,conn);DataTable dt=new DataTable();conn.Open();da.Fill(dt);conn.Close();

⑨ winform 連接資料庫代碼

//連接封裝一下,具體的增刪改查具體寫
public class OracleConn
{
public static OracleConnection Conn()
{
try
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
OracleConnection conn = new OracleConnection(constr);
return conn;
}
catch (Exception ex)
{
MessageBox.Show("資料庫連接不成功!", ex.Message);
return null;
}
}
}

⑩ .net winform怎麼連接mysql

方法/步驟1

  • 先從左側的工具欄拖進控制項Button和RichTextBox。