本文介绍了我必须在c#中加入两个表,查询在sql中工作但不在c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是订单附近的语法错误







The error is that "Incorrect syntax near Order"



private void Window_Loaded(object sender, RoutedEventArgs e)
        {
         
 string ID = textBox1.text;
            string Contact = textBox2.Text;
            string Nme = textBox3.Text;
            string Cab = textBox3.Text;

            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BingMap\WpfApplication1\WpfApplication1\Database1.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select u.Phone,u.Name,o.CabType from Customer u join Order o on u.ID=o.OrderID where ID='" + lbid + "'", con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox2.Text = dr[1].ToString();
                textBox3.Text = dr[2].ToString();
                textBox3.Text = dr[1].ToString();

            }
            else
            {
                MessageBox.Show("Data not found");
            }
            con.Close();
        }

推荐答案


SqlCommand cmd = new SqlCommand("Select u.Phone,u.Name,o.CabType from Customer u join Order o on u.ID=o.OrderID where ID=@ID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID",lbid);
//code to fetch 


这篇关于我必须在c#中加入两个表,查询在sql中工作但不在c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 13:47