[英]How can i make my project take one column so i can use it in the insert into values from the other window with select from
let me explain the idea of my simple project so you can get my question.. I have a multi login page first page is student and the other admin in the login page the values are set as select from and in the student it is set as insert into so the problem is the student will enter his window and make a request and it will go back to the database but the problem that i'm facing now i can't tell which student made the request.让我解释一下我的简单项目的想法,这样你就可以得到我的问题了。我有一个多登录页面,第一页是学生,登录页面中的另一个管理员的值设置为 select 从学生中设置为插入所以问题是学生将输入他的 window 并提出请求,它会将 go 返回到数据库,但我现在面临的问题是我不知道是哪个学生提出了请求。 i need somehow to take the column username from the login table which is set as select from and make it into the insert into in the student table !
我需要以某种方式从设置为 select 的登录表中获取列用户名,并将其插入到学生表中! i have no idea how to do that but i hope you get my question i will put the coding for the reference and excuse my rookie question and coding .
我不知道该怎么做,但我希望你能得到我的问题,我会把编码作为参考,并原谅我的菜鸟问题和编码。
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
string query = "SELECT count (1) FROM Login WHERE Username=@Username AND password=@password AND usertype=@usertype";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
sqlCmd.Parameters.AddWithValue("@password", txtPassword.Text);
if (checkbox1.IsChecked == true)
sqlCmd.Parameters.AddWithValue("@usertype", checkbox1.Content.ToString());
else if (checkbox2.IsChecked == true)
sqlCmd.Parameters.AddWithValue("@usertype", checkbox2.Content.ToString());
int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
if (count == 1)
{
if ((bool)checkbox1.IsChecked == true)
{
MainWindow Dashboard = new MainWindow();
Dashboard.Show();
MessageBox.Show("You're logged in as an Admin");
this.Close();
}
if ((bool)checkbox2.IsChecked == true)
{
Student stu = new Student();
stu.Show();
MessageBox.Show("You're logged in as a Student");
this.Close();
}
}
///the Student window ///学生window
private void button_Click(object sender, RoutedEventArgs e)
{
using (var Con = new SqlConnection(@"Data Source = test\SQLEXPRESS; Initial Catalog =Log-In; Integrated Security = True"))
{
Con.Open();
var table = "INSERT INTO Student(DepartureCiry,ArrivalCity,DateOfDeparture,DateOfReturn)VALUES(@DepartureCiry,@ArrivalCity,@DateOfDeparture,@DateOfReturn)";
using (var cmd = new SqlCommand(table, Con))
{
cmd.Parameters.AddWithValue("@DepartureCiry", fromtxt.Text);
cmd.Parameters.AddWithValue("@ArrivalCity", Totxt.Text);
cmd.Parameters.AddWithValue("@DateOfDeparture", date_time_picker.Text);
cmd.Parameters.AddWithValue("@DateOfReturn", date_time_picker1.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data inserted suckssfully");
You need to change your query to be您需要将查询更改为
string query = "SELECT Username FROM Login WHERE Username=@Username AND password=@password AND usertype=@usertype";
Instead of selecting only the count(1)
you select the actual row (in my case I only select the Username
column as you only need it).而不是只选择
count(1)
您 select 实际行(在我的情况下,我只选择 select Username
名列,因为您只需要它)。
Then instead of calling ExecuteScalar
on the sql command you need to use a SqlDataReader
to actually retrieve the row.然后,您需要使用
SqlDataReader
来实际检索行,而不是在 sql 命令上调用ExecuteScalar
。
SqlDataReader reader = sqlCmd.ExecuteReader();
if (reader.HasRows)
{
string userName;
while (reader.Read())
{
userName = ((IDataRecord)reader)["Username"].ToString();
break;
}
}
From here you have your student name from the Login table.从这里您可以从登录表中获得您的学生姓名。 However my observation is that this name will actually be the same as the
txtUsername.Text
.但是我的观察是,这个名称实际上与
txtUsername.Text
相同。 I think you need some other columns from the Students
table, but generally this is the way to retrieve them - user SqlDataReader
, and retrieve the column with the ((IDataRecord)reader)["MyColumnName"]
.我认为您需要
Students
表中的其他一些列,但通常这是检索它们的方法 - 用户SqlDataReader
,并使用((IDataRecord)reader)["MyColumnName"]
检索列。
Some additional notes: I use HasRows
property on the reader to see if it has a match instead of getting the count.一些附加说明:我在阅读器上使用
HasRows
属性来查看它是否匹配而不是获取计数。 And I imply that you have unique Usernames in the database (otherwise you have to loop the reader to see how many rows it return).而且我暗示您在数据库中有唯一的用户名(否则您必须循环阅读器以查看它返回的行数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.