求救一个c#问题

来源:百度知道 编辑:UC知道 时间:2024/05/11 17:03:35
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string id;//用户名
string name;//姓名
string studentNo;//学号
string comstring = "data source=.;initial catalog=myschool;user id=sa;pwd=123";
SqlConnection connection = new SqlConnection(comstring);
string sql = string.Format("select Loginid,studentname,studentNO from student where Loginid like '%{0}%'", textBox1.Text);
//创建command对象

是数据类型的问题。

string id;
id = (string)reader["Loginid"];
lvistudent.Tag = (int)reader["Loginid"];

看你表中Loginid存储的是什么类型的数据了。
如果是数值类型,在第二句就会出错。
如果是字符串类型在第三句就会出错。

string sql = string.Format("select Loginid,studentname,studentNO from student where Loginid like '%{0}%'", textBox1.Text);

试试改下这个参数 '%{0}%' ======= "%"+‘{0}’+"%"

首先说明你的异常是什么

通过看你的代码,推测你的Id是整型的 所以sql在查询的时候也许有点问题,但是不能像楼上说得那样加逗号解决,直接写成= 把%去掉也许解决问题
想模糊查询的话最好修改数据类型,否则只能这样比较 把textBox1.Text转换为int,设为A(Convert.toint32(textBox1.Text)即可)看看有没有等于A的,有没有等于10*A的 有没有100*A的……一直到10的n次方大于最大的id为止

写出异常
要不然搞不明白的