導航:首頁 > 數據分析 > linq怎麼連接資料庫

linq怎麼連接資料庫

發布時間:2023-08-13 10:57:07

⑴ 使用Linq查詢資料庫的問題

varlst=(fromcinquanxian.View_人員信來息自
wherec.工號==this.gh.Text.Trim()&&c.密碼==this.pwd.Text
selectc.許可權).ToList();

if(lst.Count<0)
{
varentity=lst[0];
varright=entity.許可權;
switch(right)
{
case0:
break;
case1:
break;
//case2,3,4......
default:
break;
}
}


望採納

⑵ 怎樣調用資料庫的數據

你這個問題太大了。有很多方法。就程序設計語言來說,很多程序設計語言都有調用資料庫的命令。一般地,先建立資料庫連接,再用命令調用SQL語句獲取資料庫的數據。對於.net的linq,操作更方便,功能更強大。

⑶ C#用LINQ操作Datatable資料庫

假設你上圖表格數據為 DataTable dt,列分別為AA,BB,CC,DD

var query = from p in dt.AsEnumerable()
//分組
group p by new
{
t1 = p.Field<int>("AA").ToString().Substring(0, p.Field<int>("AA").ToString().Length - 2),
t2 = p.Field<double>("BB"),
t3 = p.Field<string>("CC")
} into g
select new
{
ColA = g.Key.t1,
ColB = g.Key.t2,
ColC = g.Key.t3,
ColD = g.Sum(c => c.Field<int>("DD"))
};

//轉datatable
DataTable dtNew = dt.Clone();
DataRow drNew;
foreach (var p in query)
{
drNew = dtNew.NewRow();
drNew["AA"] = p.ColA;
drNew["BB"] = p.ColB;
drNew["CC"] = p.ColC;
drNew["DD"] = p.ColD;

dtNew.Rows.Add(drNew);
}

dtNew 為最終結果

註:轉換過程中要考慮空值的情況

⑷ 怎麼在本機連接虛擬機里的資料庫

虛擬機和物理機網卡橋接,能ping通就可以訪問,你可以從你虛擬機網卡設置里看看是不是橋接Bridge。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace DataInput
{
public class DataInputClass
{
private string instrName="";
private string ConStr = "server=.;database=SerialData;uid=sa;pwd=880906";
//資料庫對象
SqlConnection conn = null;
SqlCommand cmd = new SqlCommand();

char separator = '^';
string[] myResult = new string[10];

public DataInputClass(string myInstrName)
{
instrName = myInstrName;
}
public string InstrName
{
get
{
return instrName;
}
}

public string myDataInput(string myInstrCode)
{

//打開資料庫
try
{
conn = new SqlConnection(ConStr);
conn.Open();

if (!(conn.State == ConnectionState.Open))
{
MessageBox.Show("資料庫打開失敗!");
}
}
catch(Exception e)
{
MessageBox.Show("資料庫打開異常!"+ e.Message);
return "";
}
//數據入庫
try
{
myResult = myInstrCode.Split(separator);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into SerialResult values(" + "\'" + myResult[0] + "\',\'" + myResult[1] + "\',\'" + myResult[2] + "\')";
cmd.ExecuteNonQuery();
cmd.Dispose();
return "";
}
catch(Exception e)
{
MessageBox.Show("數據插入失敗異常!" + cmd.CommandText+"|"+e.Message);
return "";
}

}

}
}

⑸ linq如何設置連接資料庫的字元串

你用的是LinqToSql吧?在db = new NewDBDataContext(「指定的鏈接字元串」);這是使用有一個參數的構造函數,或者看無參的構造函數:
public NewDBDataContext() : base(global::NewGasgoo.AutoRepost.Properties.Settings.Default.DBConnectionString, mappingSource)
{
OnCreated();
}
看這個NewGasgoo.AutoRepost.Properties.Settings.Default.DBConnectionString的定義:
public string DBConnectionString {
get {
return ((string)(this["DBConnectionString8"]));
}
}
這個值就是Setting里的配置鏈接。
你可以將這個Return 修改為自己的配置,比如ConfigurationManager.AppSettings["DBString"],
也可以直接修改Setting配置。不過我比較推薦db = new NewDBDataContext(「指定的鏈接字元串」)。因為如果重新托表, return ((string)(this["DBConnectionString8"]));就會自動變的。

閱讀全文

與linq怎麼連接資料庫相關的資料

熱點內容
win10錯誤2系統找不到指定文件 瀏覽:327
U盤啟動載入資源文件失敗 瀏覽:459
怎樣把ug的pdf文件導入cad 瀏覽:148
c盤文件刪不了怎麼辦 瀏覽:248
win7無線網路禁用 瀏覽:455
如何把電腦文件夾圖片放置在一起 瀏覽:702
如何對訪談法進行數據分析 瀏覽:388
國土三調文件用什麼軟體什麼打開 瀏覽:651
數控銑床編程特點是什麼 瀏覽:119
飛鴿傳輸一次能傳多少文件 瀏覽:488
手機照片許可權怎麼設置密碼 瀏覽:341
es文件瀏覽器怎麼隱藏 瀏覽:649
刪除的文件為什麼恢復 瀏覽:314
血緣詛咒怎麼升級 瀏覽:604
文件分享到微信怎麼操作 瀏覽:393
tmx文件trados 瀏覽:704
大數據與會計選課怎麼選 瀏覽:684
網路的不穩定因素有哪些 瀏覽:950
獵豹瀏覽器javascript 瀏覽:723
哪個列印軟體保存數據時間長 瀏覽:873

友情鏈接