问题描述
我刚学C#不久,写了一段代码,在编译时没有错误,一运行时就提示错误,请各位高手帮忙改正一下,万分感谢!下面是那段代码:privatevoidbutton1_Click(objectsender,EventArgse){stringsql=string.Format("select*fromvegtable1wherevegnolike'%{0}%',vegnamelike'%{1}%'",txtname1.Text,txtno2.Text);try{DBHelper.con.Open();SqlCommandcmd=newSqlCommand(sql,DBHelper.con);SqlDataReaderreader=cmd.ExecuteReader();运行时这一行提示附近有语法错误!lvData.Items.Clear();if(reader.Read()){stringno=(string)reader[0];stringname=(string)reader[1];//intdate=(int)reader[2];ListViewItemitem=newListViewItem(name);item.SubItems.AddRange(newstring[]{no.ToString(),name});lvData.Items.Add(item);}reader.Close();}catch(Exceptionex){MessageBox.Show(ex.Message);}finally{DBHelper.con.Close();}}
解决方案
解决方案二:
楼主不给分的话就没动力回答了··呵呵
解决方案三:
stringsql=string.Format("select*fromvegtable1wherevegnolike'%{0}%',vegnamelike'%{1}%'",txtname1.Text,txtno2.Text);应改为:stringsql=string.Format("select*fromvegtable1wherevegnolike'%{0}%'andvegnamelike'%{1}%'",txtname1.Text,txtno2.Text);
解决方案四:
publicstaticvoidBB(){SqlDataReadermyReader=null;SqlConnectionmySqlConnection=newSqlConnection("数据连接");SqlCommandmySqlCommand=newSqlCommand("select*fromcustomers",mySqlConnection);try{mySqlConnection.Open();myReader=mySqlCommand.ExecuteReader();while(myReader.Read()){Console.Write(myReader["CustomerID"].ToString()+"");Console.WriteLine(myReader["CompanyName"].ToString());}}catch(Exceptione){Console.WriteLine(e.ToString());}finally{if(myReader!=null)myReader.Close();if(mySqlConnection.State==ConnectionState.Open)mySqlConnection.Close();}}
这写没有错~~
解决方案五:
哦~~原来是sql语句有语法错误。。。。。