前言:
在開發專案的時候,常常會需要儲存/載入參數的功能,有時候還會需要做到查詢資料的功能,
當然,如果依照想要有查詢資料的功能來看,
.NET裡面已經內建linq的功能可以使用,但是遇到要將資料存檔/載入的時候,會發現linq可能不是這麼方便,
一般在處理這類的任務的時候,常常會使用SQL來輔助儲存,但是無論是MySQL/MSSQL,體積都蠻巨大的,
而且安裝起來不方便之外,也無法做到資料的可攜性,.NET裡面有出了一個輕量化,並且檔案可以隨時帶著走的LocalDB,
LocalDB既可以擁有SQL的便利之外,也在存檔時只需要存入一個實體檔案。
安裝:
除了安裝Visual Studio之外,還需要安裝SQL Server Express LocalDB,如下圖:開發步驟:
1. 新增服務架構資料庫(mdf檔)
4. 在程式碼內連接LocalDB的資料庫,其中SqlConnection是歸類在System.Data.SqlClient的命名空間裡面
完整的source code如下:
使用範例:
string local_db_path = @"./Database1.mdf"
this.mCONN_STR = string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""{0}"";Integrated Security=True", local_db_path);
this.conn = new SqlConnection(this.mCONN_STR);
try
{
this.conn.Open();
}
catch (Exception ex)
{
throw ex;
}
5. 連接完成後就可以開始做新增/查詢資料,其中cmdText為MSSQL的SQL命令(非查詢,可以是新增/更新/刪除等指令)
SqlCommand scom = new SqlCommand("", this.conn);
scom.CommandText = cmdText;
try
{
int a = scom.ExecuteNonQuery();
if (a > 0)
{
Console.WriteLine("test -> {0}", a);
}
return true;
}
catch
{
return false;
}
6. 查詢資料DataTable tab = new DataTable();
SqlCommand scom = new SqlCommand("", this.conn);
scom.CommandText = CommandText;
SqlDataReader sread = scom.ExecuteReader();
for (int i = 0; i < sread.FieldCount; ++i)
{
tab.Columns.Add(sread.GetName(i));
}
while (sread.Read())
{
var n_row = tab.NewRow();
for (int i = 0; i < sread.FieldCount; i++)
{
n_row[i] = sread.GetValue(i);
}
tab.Rows.Add(n_row);
}
sread.Close();
完整的source code如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace DataBindingTest
{
//
// A class to connect local mssql server file
//
class local_sql_connector
{
// connect to localdb string
string mCONN_STR = @"";
// localdb connection
SqlConnection conn;
public local_sql_connector(string local_db_path)
{
this.mCONN_STR = string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""{0}"";Integrated Security=True", local_db_path);
this.conn = new SqlConnection(this.mCONN_STR);
try
{
this.conn.Open();
}
catch (Exception ex)
{
throw ex;
}
}
///
/// insert data to localdb
///
/// full text of sql insert command
public bool InsertData(string cmdText)
{
SqlCommand scom = new SqlCommand("", this.conn);
scom.CommandText = cmdText;
try
{
int a = scom.ExecuteNonQuery();
if (a > 0)
{
Console.WriteLine("test -> {0}", a);
}
return true;
}
catch
{
return false;
}
}
///
/// get data from localdb
///
/// full text of sql select command
///
public DataTable GetData(string CommandText)
{
DataTable tab = new DataTable();
SqlCommand scom = new SqlCommand("", this.conn);
scom.CommandText = CommandText;
SqlDataReader sread = scom.ExecuteReader();
for (int i = 0; i < sread.FieldCount; ++i)
{
tab.Columns.Add(sread.GetName(i));
}
while (sread.Read())
{
var n_row = tab.NewRow();
for (int i = 0; i < sread.FieldCount; i++)
{
n_row[i] = sread.GetValue(i);
}
tab.Rows.Add(n_row);
}
sread.Close();
return tab;
}
}
}
使用範例:
this.local_db = new local_sql_connector(string.Format(@"{0}\{1}", Application.StartupPath, "Database1.mdf"));
this.local_db.InsertData("INSERT INTO [dbo].[Table] ([Id], [Title], [Path]) VALUES (4, N'dfgk', N'erwqse')");
var records = this.local_db.GetData("SELECT * from [dbo].[Table] ");
沒有留言:
張貼留言