多部分标识符无法绑定C# [英] The multi-part identifier could not be bound C#

查看:59
本文介绍了多部分标识符无法绑定C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void prijavaAction()
       {
           if (textBox1.Text.All(char.IsDigit))

           {
               string selektujradnika = "SELECT ime_prezime  FROM radnici WHERE (bar_kod LIKE @FIND)";// pronaci radnika u bazi
               SqlCommand osobaId = new SqlCommand(selektujradnika);





               con = new SqlConnection(cs);
               con.Open();
               con.Close();


               {

                   con.Open();
                   osobaId = new SqlCommand(selektujradnika);
                   osobaId.Connection = con;


                   osobaId.Parameters.Add(new SqlParameter("@Find", System.Data.SqlDbType.VarChar, 50, "bar_kod")).Value = textBox1.Text;


                   osobaId.Parameters["@Find"].Value = textBox1.Text;
                   rdr = osobaId.ExecuteReader();
                   messageLabel.Text = " ";
                   readerresultTextbox.Text = " ";








                   if (rdr.Read())   //prijava radnika

                   {
                       notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFE11A");
                       messageLabel.Text = (rdr["ime_prezime"].ToString()) + "  je prijavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
                       readerresultTextbox.Text = (rdr["ime_prezime"].ToString());

                       con.Close();

                       string selektujradnika2 = "SELECT ime_prezime  FROM radnici WHERE (bar_kod LIKE @Find)";
                       SqlCommand osobaId2 = new SqlCommand(selektujradnika2);
                       con.Open  ();

                       string provjeraprijavestring = "SELECT COUNT (*) FROM dbo.prijava_radnika WHERE radnik= " + osobaId + " and vrijemeodjave is null and status='prijavljen'";// pronaci radnika u bazi
                                                                                                                                                                                    //string provjeraprijavestring2 = "SELECT count(*) FROM dbo.prijava_radnika WHERE vrijemeodjave IS NULL and status = 'prijavljen'";// pronaci radnika u bazi
                       SqlCommand komandaprijave = new SqlCommand(provjeraprijavestring, con);

                       int userCount = (int)komandaprijave.ExecuteScalar();

                       if (userCount > 0)
                       {

                           ///////////////////////////////////
                           using (SqlConnection openCon3 = new SqlConnection(cs))
                           {


                               SqlCommand comm = new SqlCommand(cs);

                               comm.CommandText = "select max(brojprijavnice2) from dbo.prijava_radnika";
                               comm.Connection = openCon3;
                               openCon3.Open();
                               rdr = comm.ExecuteReader();
                               int max = 0;
                               while (rdr.Read())
                               {
                                   max = Convert.ToInt32(rdr[0].ToString());
                                   Console.WriteLine(max);

                               }
                               openCon3.Close();


                               String saveStaff = "UPDATE dbo.prijava_radnika SET vrijemeodjave=@vrijemeodjave, brojprijavnice2=" + (max + 1) + " WHERE radnik=" + osobaId;
                               using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                               {
                                   querySaveStaff.Connection = openCon3;
                                   querySaveStaff.Parameters.Add("@vrijemeodjave", SqlDbType.DateTime).Value = DateTime.Now;

                                   openCon3.Open();
                                   querySaveStaff.ExecuteNonQuery();
                                   openCon3.Close();

                                 //  osobaId = new SqlCommand(selektujradnika);
                                 //  osobaId.Connection = con;




                                   notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#76AB01");
                                   messageLabel.Text = selektujradnika + " je odjavljen u\n" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
                                   textBox1.Text = "";

                               }


                           }
                       }
                       else
                       {

                           string saveStaff = "INSERT into dbo.prijava_radnika (vrijemeprijave, radnik, status) VALUES(@vrijemeprijave,@radnik,'prijavljen')";
                           using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))


                           {

                               querySaveStaff.Connection = con;
                               querySaveStaff.Parameters.Add("@vrijemeprijave", SqlDbType.DateTime).Value = DateTime.Now;
                               // querySaveStaff.Parameters.Add("@vrijemeodjave", SqlDbType.DateTime).Value = vrijemeodjaveDateTimePicker;
                               //querySaveStaff.Parameters.Add("@brojprijavnice", SqlDbType.VarChar, 255).Value = brojprijavnice2TextBox.Text;
                               querySaveStaff.Parameters.Add("@radnik", SqlDbType.VarChar, 255).Value = readerresultTextbox.Text;
                               // querySaveStaff.Parameters.Add("@id_fakture", SqlDbType.Int).Value = id_fakture;

                               con.Open();
                               querySaveStaff.ExecuteNonQuery();
                               con.Close();
                               osobaId = new SqlCommand(selektujradnika);
                               osobaId.Connection = con;
                           }

                       }

                       con.Close();
                   }




                   else
                   {
                       notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
                       messageLabel.Text = "Ne postoji radnik sa ovim bar kodom";
                       textBox1.Text = "";


                   }
               }
           }
           else
           {
               notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
               messageLabel.Text = "Bar kod nije važeći ili nije bar kod";
               textBox1.Text = "";


           }




       }





我尝试了什么:



多部分标识符,有什么问题?



What I have tried:

Multi part indentifier, what si a problem?

推荐答案

这是SQL抱怨某些表或列名称不存在。

做两件事:

1)使用调试器找出哪个SQL语句给出了错误。

2)仔细查看错误消息,它通常会告诉您无法找到的项目的名称:

It's SQL complaining that some table or column name does not exist.
Do two things:
1) Use the debugger to find out which SQL statement is giving the error.
2) Look closely at the error message, which normally tells you the name of the item it can't locate:
The multi-part identifier "blah.blah" could not be bound.

然后仔细查看您找到的SQL - 很可能拼错了一个名字。



Then look closely at the SQL you located - chances are you misspelled a name.

Quote:

调试没有错误。当启动应用程序和测试错误是:,,多部分标识符System.Data.SqlClient.SqlCommand无法绑定。,,

Debug no errors. When start app and test error is: ,,The multi-part identifier "System.Data.SqlClient.SqlCommand" could not be bound.,,





现在看看你的代码:



Now look at your code:

SqlCommand osobaId = new SqlCommand(selektujradnika);
...
string provjeraprijavestring = "SELECT COUNT (*) FROM dbo.prijava_radnika WHERE radnik= " + osobaId + " and vrijemeodjave is null and status='prijavljen'";
SqlCommand komandaprijave = new SqlCommand(provjeraprijavestring, con);
...
int userCount = (int)komandaprijave.ExecuteScalar();

所以你要连接一个字符串来形成一个SQL命令 - 这总是一个坏主意 - 并尝试使用SqlCommand对象作为值来寻找?

对象的默认ToString实现是对象的全名,因此在这种情况下,传递给SQL的命令变为:

So you are concatenating a string to form an SQL command - which is ALWAYS a bad idea - and trying to use an SqlCommand object as a value to look for?
The default ToString implementation for an object is the full name of the object, so in this case your command as passed to SQL becomes:

SELECT COUNT (*) FROM dbo.prijava_radnika WHERE radnik= System.Data.SqlClient.SqlCommand and vrijemeodjave is null and status='prijavljen'

并且SQL Server假定您正在尝试在SQL中指定唯一对象引擎。





看看你给出整个错误信息有多大帮助?


顺便说一下:Debug no errors是错误的:编译不是调试。调试是在应用程序运行时查看应用程序并收集有关它正在执行的操作的信息的过程。如果您已在调试器中运行此代码,它会告诉您treh问题是在行

And SQL Server assumes that you are trying to specify a unique object within the SQL engine.


See how much it helps when you give the whole error message?

And by the way: "Debug no errors" is wrong: Compiling is not debugging. Debugging is the process of looking at an application while it is running, and gathering information about what it is doing. If you have run this code in the debugger, it would have told you treh problem was on the line

int userCount = (int)komandaprijave.ExecuteScalar();

并且您可以拥有直接查看命令字符串并看到问题 - 甚至修复它并再试一次!

and you could have looked at the command string directly and seen the problem - even fixed it and tried again!


这篇关于多部分标识符无法绑定C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆