日期:2014-05-18  浏览次数:20446 次

ASP.NET C# SQL数据库连接
数据库有两个表,一个点菜的表,将点菜的价格写进去和一个菜单的表,这是我编写的如下代码,就是不知道哪里错,希望大家帮帮忙
C# code

            System.Data.SqlClient.SqlConnection conn = new SqlConnection(); 
            conn.ConnectionString =
                ConfigurationManager.ConnectionStrings["餐厅点菜ConnectionString"].ConnectionString;
            conn.Open();          
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = conn;          
            conn.Close();
            
            string SqlStr;
            SqlStr = "Insert into [点餐]([名称])values(@名称)";
            Cmd.CommandText = SqlStr;
            SqlParameter para1 = new SqlParameter("@名称", SqlDbType.VarChar, 50);
            para1.Value = ListBox1.SelectedValue;
            Cmd.Parameters.Add(para1);

            conn.Open();
            Cmd.ExecuteNonQuery();
            conn.Close();

            System.Data.SqlClient.SqlConnection conn1 = new SqlConnection();
            conn1.ConnectionString =
                ConfigurationManager.ConnectionStrings["餐厅点菜ConnectionString"].ConnectionString;
            conn1.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn1;
            conn1.Close();

            string price = "UPDATE [点餐] SET [价格] = (select 价格 from 菜单 where 名称=(select 名称 from 菜单))";
            cmd.CommandText = price;

            SqlParameter para = new SqlParameter("@价格", SqlDbType.VarChar, 50);

            para.Value = price;
            cmd.Parameters.Add(para);
            conn1.Open();
            cmd.ExecuteNonQuery();
            conn1.Close();


 

------解决方案--------------------
string price = "UPDATE [点餐] SET [价格] = (select 价格 from 菜单 where 名称=(select 名称 from 菜单))";
cmd.CommandText = price;

SqlParameter para = new SqlParameter("@价格", SqlDbType.VarChar, 50);//此處你用了参数@价格,可你上面的SQL哪里有?

------解决方案--------------------
1,你要么就要跟二楼的同志把@价格先查出来,然后用
string price = "UPDATE [点餐] SET [价格] =@价格"
2,要么你//SqlParameter para = new SqlParameter("@价格", SqlDbType.VarChar, 50);注掉