1. 在项目中-新建项-创建LINQ to SQL文件 设命名为 myData.dbml
2. 在服务器资源管理器中,把使用的SQL文件 表 拖入myData.dbml中;
代码
private void QueryDatabyLINQ()
{
//myData 为文件名
myDataDataContext lq = new myDataDataContext();
var mylq = from n in lq.Student select n;
foreach (var n in mylq)
{
this.Response.Write(n.S_Class.ToString()+"<hr/>");
}
}
private void QueryDatabySQL()
{
try
{
SqlConnection con = new SqlConnection("server='(local)';database='hahacjh';uid='sa';pwd='hahacjh';");
con.Open();
string strSql = "select * from Student ";
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
int j = da.Fill(ds, "mytable");
con.Close();
//方法1:
//for (int i = 0; i < j; i++)
//{
// this.Response .Write (ds.Tables["mytable"].Rows[i]["S_Class"].ToString()+"<hr/>");
//}
//方法2:
DataTable dt = ds.Tables["mytable"];
var dslq = from d in dt.AsEnumerable() select d;
foreach (var data in dslq)
{
this.Response.Write(data.Field<string>("S_Class").ToString() + "<hr/>");
}
}
catch
{
Console.WriteLine("数据库连接有误");
}