| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 737 人关注过本帖
标题:C#与SQL类型 转换
只看楼主 加入收藏
jianghong_02
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:143
专家分:476
注 册:2010-10-2
结帖率:100%
收藏
 问题点数:0 回复次数:0 
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();
            }
        }
    }
}
搜索更多相关主题的帖子: SQL 类型 
2010-10-02 13:08
快速回复:C#与SQL类型 转换
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073953 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved