導航:首頁 > 數據分析 > 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怎麼連接資料庫相關的資料

熱點內容
編程圖元屬性怎麼修改 瀏覽:828
word文檔的大括弧怎麼輸入 瀏覽:862
關於旅遊收入的數據從哪裡找 瀏覽:989
gson解析json集合問題 瀏覽:680
安卓ipsec標識符填什麼 瀏覽:215
在哪裡解壓文件第一分卷 瀏覽:63
奧維使用教程 瀏覽:324
編程程序怎麼轉到plc上 瀏覽:807
文件名沖突但是找不到 瀏覽:261
上海瑞金醫院app下載 瀏覽:998
qq群里的機器人買武器 瀏覽:428
捕魚達人歷史版本 瀏覽:73
mp4視頻文件解密軟體 瀏覽:62
多軸編程哪個軟體最方便 瀏覽:27
老平板哪個是顯示屏數據線插座 瀏覽:849
5sing上傳音頻文件格式 瀏覽:171
win10輸入文件滑鼠右鍵異常 瀏覽:634
聽幼兒故事用什麼app 瀏覽:514
iphone修改音頻文件名 瀏覽:53
國家氣象站點數據在哪裡下載 瀏覽:342

友情鏈接