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

熱點內容
excel表格文件化亂碼 瀏覽:986
win7無法打開隱藏文件 瀏覽:757
怎樣把壓縮文件變成空白文件 瀏覽:516
wokrNC和UG編程哪個好 瀏覽:487
愛奇藝隨刻文件名 瀏覽:40
wps是怎麼壓縮文件 瀏覽:986
dos配置文件名 瀏覽:582
java軟體工程師做什麼 瀏覽:897
3dmax導出ive文件 瀏覽:464
數據重復性好是什麼意思 瀏覽:166
如何查找電腦文件使用歷史記錄 瀏覽:821
linux可以運行exe的文件嗎 瀏覽:935
庫幣是哪個國家的app 瀏覽:201
大智慧internet文件夾有哪些文件 瀏覽:89
編程培訓課程哪個排名好 瀏覽:124
我的cad怎麼沒顯示文件選項卡 瀏覽:952
命令行當前是哪個資料庫 瀏覽:588
為什麼appstore打開空白 瀏覽:574
選貨車軟體什麼app最好 瀏覽:577
psv仙境傳說ace如何降級版本 瀏覽:460

友情鏈接