数据库级联查询

数据库级联查询,第1张

1.提取单条记录

//using System.Data

//using System.Data.SqlClient

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

SqlCommand cmd = new SqlCommand("Select Count(*) From jobs",cn)

cn.Open()

%%2=cmd.ExecuteScalar()//Message.InnerHtml

}2.单值比较

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

SqlCommand cmd = new SqlCommand("Select Count(*) From jobs",cn)

cn.Open()

%%2=cmd.ExecuteScalar()//Message.InnerHtml

if(%%2==%%3)

{

%%4

}

}

3.显示表格

//using System.Data

//using System.Data.SqlClient

DataSet ds=null

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

cn.Open()

cmd=new SqlDataAdapter("Select * From Author",cn)

ds=new DataSet()

cmd.Fill(ds,%%2)//"作者"

MyDataGrid.DataSource=ds.Tables(%%2).DefaultView//"作者"

MyDataGrid.DataBind()

}

4. *** 作表格

//using System.Data

//using System.Data.SqlClient

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

cn.Open()

cmd=new SqlDataAdapter("Select * From Author",cn)

ds=new DataSet()

cmd.Fill(ds,%%2)//"作者"

MyDataGrid.DataSource=ds.Tables(%%2).DefaultView//"作者"

MyDataGrid.DataBind()

}

5.数值范围查询

//using System.Data

//using System.Data.SqlClient

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

}

6.关闭时断开连接

//using System.Data

//using System.Data.SqlClient

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

}

7.执行命令

//using System.Data

//using System.Data.SqlClient

using (SqlConnection cn = new SqlConnection(%%1))

//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString

//Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword

{

SqlCommand cmd = new SqlCommand("insert userRegister_t values('"

+ TextBox1.Text + "','" + TextBox2.Text + "')",cn)

cn.Open()

cmd.ExecuteNonQuery()

}

最好用inner join 来联表 那样语句可以清晰点

但是我看了下 你语句似乎也没有错 太复杂了 不容易看得出

我给你写一个你先试试

select class_code,class.dptmt_code,dptmt_name,yxnm,snum from

class as c inner join dptmt as d on(c.dptmt_code=d.dptmt_code)

inner join yx as y on(y.yxid=d.yxid)

这一句就已经帮你把三表连接号了 后面自己再加条件

先试试这句代码查得出所有东西不 然后再加条件

但是 我得说你把数据库 *** 作放在那里是不好的 数据库 *** 作最好用类封装好 然后调用就是了 你那样既防不了sql注入又容易出问题 还不好检查 扩展性也很差 虽然看不出是asp 还是aspx 还是jsp 或者其他 但是 你那样做是不行的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6704467.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存