gpt4 book ai didi

sql - 什么使两个表相关?

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:02 24 4
gpt4 key购买 nike

我是 web 开发 asp.net C# 的新手。我知道给外键(主键 1 表),然后我们加入这些表。但是当我们加入他们时会发生什么? (我不知道如何加入表格)我尝试阅读一些文章,但最终没有正确理解它。感谢任何形式的帮助。
这就是我使用两个表的方式。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Result : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd,cmd1;
SqlDataReader dr;

string city1, area1, type1, min, max;
int id = 0;
int id2 = 0;
protected void Page_Load(object sender, EventArgs e)
{
city1 = Request.QueryString["city"];
area1 = Request.QueryString["area"];
type1 = Request.QueryString["propertytype"];
// type1= "1bhk";
min = Request.QueryString["minprice"];
max = Request.QueryString["maxprice"];
id = Convert.ToInt32(Request.QueryString["uid"]);
// id = 1;
con = new SqlConnection("integrated security=true; database=data1; server=sudhir-pc");
con.Open();
//cmd = new SqlCommand("select price,area,imagename,users_id from property where city='" + city1 + "' and area='" + area1 + "' and propertytype='" + type1 + "' and users_id=" + id + "", con);
// cmd1 = new SqlCommand("select frstname,laststname,contactno from users where users_id='"+id+"'", con);
cmd = new SqlCommand("select price,area,imagename,users_id,available from property where city=@city1 and area=@area1 and propertytype=@type1", con);
cmd.Parameters.AddWithValue("@city1",city1);
cmd.Parameters.AddWithValue("@area1",area1);
cmd.Parameters.AddWithValue("@type1",type1);

// cmd.Parameters.AddWithValue("@id",id);
dr = cmd.ExecuteReader();

while (dr.Read())
{
Label1.Text = (string)dr["price"].ToString();
Label2.Text= (string)dr["area"];
string imgstr=(string)dr["imagename"];
int id1 = (int)dr["users_id"];
Image1.ImageUrl = "~/upload/"+imgstr+"";
Label13.Text = city1;
Label14.Text = type1;
Label16.Text = (string)dr["available"].ToString();

id2 = (int)dr["users_id"];
}
dr.Dispose();
cmd1 = new SqlCommand("select firstname,laststname,contactno from users where users_id=" + id2 + "", con);
dr = cmd1.ExecuteReader();
while (dr.Read())
{
Label4.Text = (string)dr["firstname"];
Label5.Text = (string)dr["laststname"];

if (id > 0)
Label6.Text = (string)dr["contactno"];


}
if (id < 1)
Label6.Text = "To see contact information. Register!";
}
}

最佳答案

使用一个外键查询两个表之间的关系示例

"SELECT * FROM Tbl1 as a JOIN Tbl2 as b ON a.PK_tbl1 = b.FK_tbl2"

PK = 主键(Tbl1 的)FK = 外键(Tbl2 的)

关于sql - 什么使两个表相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com