C#与SQL类型 转换
请教各位大侠sql中varchar()转换成C#中的整形(int) 红色区为错误区域
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyClass
{
public partial class seachStudentForm : Form
{
public seachStudentForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtName.Text =="")//判空
{
MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
FillListView();
}
}
private void FillListView()
{
string loginId;
int userStateIdo;
string name;
string studentNo;
string userState;
try
{
string sql = string.Format("SELECT loginId,userStateId,name,studentNo,phone FROM Student WHERE loginId like '%{0}%'", txtName.Text);
SqlCommand commend = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader dataReader = commend.ExecuteReader();
lvStudent.Items.Clear();
if (!dataReader.HasRows)
{
MessageBox.Show("抱歉!没你要查找的用户!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
while (dataReader.Read())//将数据库中获取的值,赋值给变量
{
loginId = (string)dataReader["loginId"];
userStateIdo = (int)dataReader["userStateId"];//是一个判断值
userState = (userStateIdo == 1) ? "活动" : "非活动";
name = (string)dataReader["name"];
studentNo = (string)dataReader["studentNo"];
ListViewItem listView = new ListViewItem(loginId);//创建一个listView项,连接视图窗口
listView.Tag = (int)dataReader["phone"];//程序员,应用可见
lvStudent.Items.Add(listView);
listView.SubItems.AddRange(new string[] { loginId, name, userState });
}
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show("抱歉!查询数据库出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
}
}