简体   繁体   English

我怎样才能让我的项目占用一列,以便我可以使用它插入另一个 window 的值和 select 来自

[英]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.

相关问题 如何创建我的MVC 5项目,使其在创建Controller时不再自动继承自ApiController? - How can I make it so that my MVC 5 project no longer automatically inherits from ApiController when creating a Controller? 如何从我的解决方案中的其他项目中使用我的Web Api项目? - How can I use my Web Api project from other projects inside my solution? 我可以使用LINQ取同一个表的两个不同的列值,并与另一个表的另一个列值进行连接吗? - Can I take two different column values of same table and connect with different one column values from another table by using LINQ? 我如何构建OleDbCommand查询,以便我可以从一个.MDB中获取表,并在另一个.MDB中替换它们。 - How do I structure an OleDbCommand Query so that I can take Tables from one .MDB, and replace them in another .MDB 我可以在一个解决方案中的其他项目中使用主项目中的类和控件吗? - Can I use classes and controls from main project in other projects within one solution? 如何从安装项目使用 - How can I use from Setup Project 我如何通过它的名称从 texboxes 中获取值 - How can i take the values from texboxes by the name of it 如何从表中获取值并将其放入每个表中 - How can I take values from a table and put it in a for each 我可以从同一列中选择多个值吗 - Can I SELECT more than one value from the same column 如何限制我的选择,使其不选择空字符串值? - How can I restrict my select so that it does not select empty string values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM