它显示==>语法错误:'Id'运算符后缺少操作数

时间:2016-08-06 08:33:17

标签: c#

namespace dt.cs
{
    class Program
    {
        static void Main(string[] args)
        {
             DataTable objdatatable;
            DataRow objdatarow;
            DataRow[] objdatarowcollection;
            int intcount,intcount1;
            try
            {
                objdatatable = new DataTable("Student Details");
                objdatatable.Columns.Add("Student ID", typeof(string));
                objdatatable.Columns.Add("Student Name", typeof(string));
                objdatatable.Columns.Add("Phone No", typeof(string));



                for (intcount = 1; intcount <= 5; intcount++)
                {
                    Console.WriteLine("enter your choice:\n 1.Add Record \n 2.Delete Record \n 3.Update Record\n 4.table rename\n 5.View Record  \n 6.exit");

                    intcount = Convert.ToInt32(Console.ReadLine());


                    switch (intcount)
                    {
                        case 1:
                            Console.WriteLine("\nHow Many Record You Want To Add:");
                            intcount1 = Convert.ToInt32(Console.ReadLine());
                            for (intcount = 1; intcount <= intcount1; intcount++)
                            {
                                objdatarow = objdatatable.NewRow();
                                Console.WriteLine("\nEnter Student Id:\n");
                                objdatarow["student Id"] = Console.ReadLine();
                                Console.WriteLine("\nEnter Student Name:\n");
                                objdatarow["student Name"] = Console.ReadLine();
                                Console.WriteLine("\nEnter student Contact Number:\n");
                                objdatarow["phone no"] = Console.ReadLine();
                                objdatatable.Rows.Add(objdatarow);
                            }

                            break;

                        case 2:

                                 Console.WriteLine("Select Id for Delete Record");
                                 string stringname = Console.ReadLine();
                                 objdatarowcollection = objdatatable.Select("student Id ='" + stringname + "'");
                                 if(objdatarowcollection!=null && objdatarowcollection.Length > 0)
                                 objdatarowcollection[0].Delete();
                                 Console.WriteLine("The Number of Records \n{0}", objdatatable.Rows.Count.ToString());
                                 break;

这是我的代码,在这里我无法删除特定记录,我犯了什么错误?

请找出我的错误朋友 提前谢谢

1 个答案:

答案 0 :(得分:0)

您需要使用[Table Name]修饰列名称,因为它由多个单词组成:

objdatarowcollection = objdatatable.Select("[student Id] ='" + stringname + "'");

旁注:这不是语法错误,而是错误的表达式语法导致的运行时错误。