Subsonic简单的语法整理
1.查詢方面
(查詢所有數(shù)據(jù)記錄[dataset讀取方法])
Myuser.Query().ExecuteDataSet().Tables[0];
Myuser.Query().ExecuteDataSet();
(返回關(guān)聯(lián)查詢[dataset讀取方法])
Myuser.Query().ExecuteDataSet().Tables[0];
Myuser.Query().ExecuteDataSet()
(返回所有的查詢記錄[DataReader讀取方法])
? List<Myuser> myu =new List<Myuser>();
???????IDataReader ida = Myuser.Query().ExecuteReader();
???????while (ida.Read())
???????{
???????????Myuser myuser = new Myuser();
???????????myuser.Userid = int.Parse(ida.GetValue(0).ToString());
???????????myuser.Username = ida.GetValue(1).ToString();
???????????myuser.Userpassword = ida.GetValue(2).ToString();
???????????myuser.Usersex = ida.GetValue(3).ToString();
???????????myu.Add(myuser);
???????}
???????GridView1.DataSource = myu;
???????GridView1.DataBind();
(返回所有的查詢記錄[dataReader讀取方式])
IDataReader ida = Myuser.FetchAll();
????????while (ida.Read())
???????{
???????????Myuser myuser = new Myuser();
???????????myuser.Userid = int.Parse(ida.GetValue(0).ToString());
???????????myuser.Username = ida.GetValue(1).ToString();
???????????myuser.Userpassword = ida.GetValue(2).ToString();
???????????myuser.Usersex = ida.GetValue(3).ToString();
???????????myu.Add(myuser);
???????}
???????GridView1.DataSource = myu;
???????GridView1.DataBind();
(帶有返回排序完畢的查詢記錄[dataReader讀取方式])
IDataReader ida = Myuser.FetchAll(OrderBy.Desc("usersex"));
IDataReader ida = Myuser.FetchAll(OrderBy.Asc("usersex"));
(根據(jù)ID返回其查詢的記錄)
Myuser.FetchByID(3).Username.ToString();
(根據(jù)查詢條件返回查詢數(shù)據(jù)記錄)
IDataReader ida=Myuser.FetchByParameter("username", "張山");
(查詢所有數(shù)據(jù)記錄[dataReader方式])
Query q = new Query("Myusers");
???????IDataReader ida=Myuser.FetchByQuery(q);
???????while (ida.Read())
???????{
???????????Myuser myuser = new Myuser();
???????????myuser.Userid = int.Parse(ida.GetValue(0).ToString());
???????????myuser.Username = ida.GetValue(1).ToString();
???????????myuser.Userpassword = ida.GetValue(2).ToString();
???????????myuser.Usersex = ida.GetValue(3).ToString();
???????????myu.Add(myuser);
???????}
???????GridView1.DataSource = myu;
???????GridView1.DataBind();
(查詢所有的數(shù)據(jù)記錄[dataReader方式])
Myuser u=new Myuser();
???????IDataReader ida = Myuser.Find(u);
???????while (ida.Read())
???????{
???????????Myuser myuser = new Myuser();
???????????myuser.Userid = int.Parse(ida.GetValue(0).ToString());
???????????myuser.Username = ida.GetValue(1).ToString();
???????????myuser.Userpassword = ida.GetValue(2).ToString();
???????????myuser.Usersex = ida.GetValue(3).ToString();
???????????myu.Add(myuser);
???????}
???????GridView1.DataSource = myu;
???????GridView1.DataBind();
(可排序查詢所有的數(shù)據(jù)記錄[dataReader方式])
Myuser u=new Myuser();
???????IDataReader ida = Myuser.Find(u,OrderBy.Asc("username"));
?
(查詢獲取listitem列表[可以于填充下拉框])
???ListItemCollection lic=Myuser.GetListItems();--默認(rèn)獲取除掉id的第一列的值
???ListItemCollection lic=Myuser.GetListItems();--可指定獲取列
??????????foreach(ListItem li in lic)
??????????{
??????????Response.Write(li.Value);
??????????}
(獲取表的結(jié)構(gòu))
Myuser.Schema
--進(jìn)行表的列的添加、刪除、判斷是否有主鍵、外鍵、等
(返回查詢的對象)
Myuser.Query().ExecuteScalar();
(可利用sql語句查詢)
?IDataReader ida = newQuery(Myuser.Schema).WHERE("userid=5").ExecuteReader();
(可以用字符串直接寫表名或者視圖名)
IDataReader rdr = newQuery("Products").WHERE("ProductID",2).ExecuteReader();
IDataReader rdr = newQuery("Products").BETWEEN_AND("DateExpires",DateTime.Now,DateTime.Now.AddDays(30).ExecuteReader();
(也可以用SubSonic定義好的結(jié)構(gòu)來表示表名或視圖名)
IDataReader rdr = newQuery(Tables.Products).AddWhere(Product.Columns.ProductID,2).ExecuteReader();
(查詢出特定條數(shù)的結(jié)果可以用Top關(guān)鍵字,也可以查詢指定的字段)
Query qry = new Query(Tables.Products);--指定表名
qry.Top = "10";--前n條查詢
qry.SelectList =Product.Columns.ProductName+","+Product.Columns.UnitPrice;--獲取指定的列名字段
qry.OrderBy =OrderBy.Desc(Product.Columns.UnitPrice);--進(jìn)行指定列名排序
(支持分頁查詢)
Query qry = new Query(Tables.Products);--指定表名
qry.PageSize = 10;--當(dāng)前頁面顯示的數(shù)據(jù)記錄數(shù)
qry.PageIndex = 2;--指定的當(dāng)前頁面
(對于多個(gè)表的連接查詢,建議使用視圖實(shí)現(xiàn),查詢條件的復(fù)合,如用OR或者IN,可以這樣查)
IDataReader rdr = newQuery("Products").WHERE("CategoryID=5").AND("UnitPrice>10").OR("CategoryID=1").And("UnitPrice>10").ExecuteReader();
(對于IN條件查詢,提供了三種參數(shù)類型:ListItemCollection,ArrayList,objectarray)
//arraylist方法
ArrayList list = new ArrayList();
for(int i =1;i<=5;i++)
???list.Add(i);
IDataReader rdr = newQuery("products").IN("ProductID",list).ExecuteReader();
/object[]
IDataReader rdr = new Query("products").IN("ProductID",newobject[]{1,2,3,4,5}).ExecuteReader();
/listitemcollection
ListItemCollection coll = new ListItemCollection();
for (int i =1;i<=5;i++)
?? ListItem item = newListItem(i.ToString(),i.ToString());
?? item.Selected = true;
?? coll.Add(item);
IDataReader rdr = newQuery("products").IN("ProductID",coll).ExecuteReader();
(執(zhí)行純SQL語句的查詢方法)
? QueryCommand qcmd = new QueryCommand("select *from Myusers");
???????DataSet ds=DataService.GetDataSet(qcmd);
???????GridView1.DataSource = ds;
???????GridView1.DataBind();
/
???????QueryCommand qcmd = newQueryCommand(Myuser.Query().GetSql());
???????DataSet ds=DataService.GetDataSet(qcmd);
???????GridView1.DataSource = ds;
???????GridView1.DataBind();
(獲取當(dāng)前的query的執(zhí)行語句)
Myuser.Query().GetSql()
(sql語句拼接查詢)
string sql = "";
Query q = new Query("vwProduct");
q.AddWhere("productID",productID);
sql = q.GetSql()+"\r\n";
q = new Query(Commerce.Common.Image.GetTableSchema());
q.AddWhere("productID",productID);
q.OrderBy = OrderBy.Asc("listOrder");
sql += q.GetSql()+"\r\n";
....
QueryCommand cmd = new QueryCommand(sql);
cmd.AddParameter("@productID",productID,DbType.Int32);
DataSet ds = DataService.GetDataSet(cmd);
(查詢統(tǒng)計(jì)結(jié)果)
SubSonic.Where w = new SubSonic.Where();
w.ColumnName = "CategoryID";
w.ParameterValue = "1";
Query.GetCount("products","productID",w);
上面的where條件是可選的,也可以直接根據(jù)表名和列名統(tǒng)計(jì),象上面的最后一句那樣。除了GetCount()方法以外,還有GetAverage()、GetSum()等方法,用法類似。
(函數(shù)查詢)
SubSonic.Where where = new Where();
???????where.ColumnName = "userid";
???????where.ParameterValue = "1";
??????Response.Write(Myuser.Query().GetCount("userid",where));?
Myuser.Query().GetCount("userid",where)--查詢當(dāng)前數(shù)的數(shù)據(jù)記錄數(shù)相當(dāng)于count(列名)+where條件
Myuser.Query().GetAverage("userid",where)--查詢當(dāng)前數(shù)據(jù)的平均值相當(dāng)于avg(列名)+where條件
Myuser.Query().GetSum("userid",where)--查詢當(dāng)前數(shù)據(jù)總和相當(dāng)于sum(列名)+where條件
Myuser.Query().GetMax("userid",where)--查詢當(dāng)前數(shù)據(jù)的最大值相當(dāng)于max(列名)+where條件
Myuser.Query().GetMin("userid",where)--查詢當(dāng)前數(shù)據(jù)的最小值相當(dāng)于min(列名)+where條件
-------where可加可不加
(多種查詢條件約定查詢)
GridView1.DataSource=Myuser.Query().WHERE("userid",Comparison.LessOrEquals, 3).ExecuteDataSet();
GridView1.DataSource=Myuser.Query().WHERE("列名", 約束條件,約束數(shù)值).ExecuteDataSet();
Comparison.LessOrEquals--小于或等于
Comparison.LessThan--小于
Comparison.BetweenAnd--等于
Comparison.Blank--填充為空白的
Comparison.OpenParentheses--打開括號(hào)【我認(rèn)為是添加"("】
Comparison.CloseParentheses--關(guān)閉括號(hào)???【我認(rèn)為是添加")"】
Comparison.Equals--字符串之間的對比相當(dāng)于等于
Comparison.NotEquals--查詢不相等的數(shù)據(jù)
Comparison.GreaterOrEquals--大于等于
Comparison.GreaterThan--大于
Comparison.In--在范圍內(nèi)取值
Comparison.Is--是否等于
Comparison.IsNot--在范圍外取值
Comparison.Like--查詢相似的數(shù)據(jù)
Comparison.NotLike--查詢不相似的數(shù)據(jù)
(創(chuàng)建一個(gè)commd查詢的連接對象)
Myuser.Query().BuildSelectCommand()相當(dāng)于QueryCommand qcmd = newQueryCommand(Myuser.Query().GetSql());
(通過Query查詢所有數(shù)據(jù))
QueryCommand qcmd = Myuser.Query().BuildSelectCommand();
???????DataSet ds = DataService.GetDataSet(qcmd);
???????GridView1.DataSource = ds;
???????GridView1.DataBind();
(去重復(fù)查詢)
DISTINCT()
(查詢指定值在表中的出現(xiàn)的次數(shù))
?Response.Write(Myuser.Destroy("usersex","男"));
(between and使用查詢方法)
?? GridView1.DataSource=Myuser.Query().BETWEEN_VALUES("userid", 1,10).ExecuteDataSet();
?? GridView1.DataBind();
---------------------------------------------------------------------------------------------------------------------------------------------
2.添加方面
方法一:
????????Myuser.Insert(txtusername.Text, txtuserpassword.Text,txtusersex.Text);
方法二:
? Myuser myuser = new Myuser();
???????????myuser.Username = txtusername.Text;
???????????myuser.Userpassword = txtuserpassword.Text;
???????????myuser.Usersex = txtusersex.Text;
???????????myuser.Save();
方法三:
int i = new Insert().Into(Myuser.Schema, "username","userpassword", "usersex").Values(TextBox1.Text, TextBox2.Text,TextBox3.Text).Execute();
//方法四
???????Myuser my = new Myuser();
???????my.SetColumnValue("username", TextBox1.Text);
???????my.SetColumnValue("userpassword", TextBox2.Text);
???????my.SetColumnValue("usersex", TextBox3.Text);
???????my.Save();
???????Response.Write("<scriptlanguage=javascript>alert('數(shù)據(jù)添加成功!');location='datashow.aspx'</script>");
//方法五
???????Insert MyuserInsert = new Insert(Myuser.Schema.TableName);
???????intresult=MyuserInsert.Into(Myuser.Schema,Myuser.UsernameColumn.ColumnName,Myuser.UserpasswordColumn.ColumnName,Myuser.UsersexColumn.ColumnName).Values(TextBox1.Text,TextBox2.Text, TextBox3.Text).Execute();
???????if (result > 0)
???????{
?????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)添加成功!');location='datashow.aspx'</script>)");
???????}
???????else
???????{
?????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)添加失敗!')</script>)");
???????}
//方法六
?QueryCommand qcmd = new QueryCommand(sql);
??????????????????????????int res=DataService.ExecuteQuery(qcmd);
3.修改方面
//方法一
???????//Myuser my = new Myuser(userid);
???????//my.Username = username;
???????//my.Userpassword = userpassword;
???????//my.Usersex = usersex;
???????//my.Save();
//方法二
???????//Query q = Myuser.Query();
???????//q.AddWhere("userid",userid);
???????//q.AddUpdateSetting("username", username);
???????//q.AddUpdateSetting("userpassword", userpassword);
???????//q.AddUpdateSetting("usersex", usersex);
???????//q.QueryType=QueryType.Update;
???????//q.Execute();
???????//Response.Write("<script>alert('"+q.GetRecordCount().ToString()+"')<script/>");
//方法三
???????//int res = newUpdate(Myuser.Schema).Set(Myuser.UsernameColumn).EqualTo(username).Set(Myuser.UserpasswordColumn).EqualTo(userpassword).Set(Myuser.UsersexColumn).EqualTo(usersex).Where(Myuser.UseridColumn).IsEqualTo(userid).Execute();
???????//if (res > 0)
???????//{
???????// //??Response.Write("<script>alert('數(shù)據(jù)修改成功')</script>");
???????//???ClientScript.RegisterStartupScript(this.GetType(),"alert","<script>alert('數(shù)據(jù)修改成功')</script>");
???????//}
???????//else
???????//{
???????//???//Response.Write("<script>alert('數(shù)據(jù)修改失敗')</script>");
???????//???ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)修改失敗')</script>");
???????//}
//方法四
???????//Myuser my = new Myuser(userid);
???????//my.SetColumnValue("username", username);
???????//my.SetColumnValue("userpassword", userpassword);
???????//my.SetColumnValue("usersex", usersex);
???????//my.Save();
?//方法五
???????Update MyuserUpdate = new Update(Myuser.Schema.TableName);
???????MyuserUpdate.From(Myuser.Schema.TableName);
???????MyuserUpdate.Where("userid").IsEqualTo(userid);
???????MyuserUpdate.Set("username").EqualTo(username);
???????MyuserUpdate.Set("userpassword").EqualTo(userpassword);
???????MyuserUpdate.Set("usersex").EqualTo(usersex);
???????int result = MyuserUpdate.Execute();
???????if (result > 0)
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)修改成功')</script>");
???????}
???????else
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)修改失敗')</script>");
???????}
//方法六
?QueryCommand qcmd = new QueryCommand(sql);
??????????????????????????int res=DataService.ExecuteQuery(qcmd);
4.刪除方面
???????//刪除的id
???????String id = GridView1.DataKeys[e.RowIndex].Value.ToString();
???????//int res = db.deldb(int.Parse(id));
//方法一
???????//Myuser.Delete(id);
//方法二
???????Query q = Myuser.Query();
???????//q.AddWhere("userid", id);
???????//q.QueryType = QueryType.Delete;
???????//q.Execute();
???????
//方法三
???????int res = newDelete(Myuser.Schema).Where("userid").IsEqualTo(id).Execute();
???????if (res > 0)
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)已經(jīng)成功刪除')</script>");
???????}
???????else
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)刪除失敗')</script>");
???????}
//方法四
???????int result = newDelete().From<Myuser>().Where(Myuser.UseridColumn).IsEqualTo(id).Execute();
???????if (result > 0)
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)已經(jīng)成功刪除')</script>");
???????}
???????else
???????{
???????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)刪除失敗')</script>");
???????}
???????
//方法五
???????Delete MyuserDelete = new Delete();
???????MyuserDelete.From(Myuser.Schema.TableName);
???????MyuserDelete.Where("userid").IsEqualTo(id);
??????int result=MyuserDelete.Execute();
??????if (result > 0)
??????{
??????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)已經(jīng)成功刪除')</script>");
??????}
??????else
??????{
??????????ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('數(shù)據(jù)刪除失敗')</script>");
??????}
//方法六
?QueryCommand qcmd = new QueryCommand(sql);
??????????????????????????int res=DataService.ExecuteQuery(qcmd);
---------------------------------------------------------------------------------------------------------------------------------------------
//執(zhí)行存儲(chǔ)過程
?StoredProcedure sps = newStoredProcedure("Pro_CheckoutCatid");//存儲(chǔ)名
??????sps.Command.AddParameter("@UserCa
總結(jié)
以上是生活随笔為你收集整理的Subsonic简单的语法整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 非平稳时间序列分析
- 下一篇: ASCII码判断大小