zoukankan      html  css  js  c++  java
  • C#程序中从数据库取数据时需注意数据类型之间的对应,int16int32int64

     private void btn2_Click(object sender, RoutedEventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test20140531;User ID=sa;Password=123"))
                {
                    conn.Open();
                    using (SqlCommand cmd2 = conn.CreateCommand())
                    {
                        cmd2.CommandText = "select * from T_Student";
                        using (SqlDataReader reader = cmd2.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                //string Name=reader.GetString(1);
                                //MessageBox.Show("姓名为:" + Name);


                                //int Age = reader.GetInt32(2);
                                //MessageBox.Show("所有人的年龄为:"+Age);


                                long Id = reader.GetInt64(0);
                                MessageBox.Show("Id值分别为:" + Id);
                                
                            }
                        }
                    }
                }

            }


    在C#程序中从数据库取数据时需注意数据类型之间的对应:(例如)

    数据库中的数据类型        C#程序中数据类型
        nvarchar                  string             (nvarchar与varchar区别就是前者可含中文)   
        smallint                  int16
        int                       int32
        bigint                    int64               (bigint数据范围较大,有可能int存储不下,所以C#中用long存储,才不报错)
    这四种是我遇到的,所以写下来,至于其他类型各位可以利用上述C#代码调试出来!
    上述代码中Id,我在Microsoft SQL Server 2008中设置为bigint类型,在C#中用long类型接收,其执行结果截图如下:
    
    

    更多知识分享:微笑空间站    
  • 相关阅读:
    window对象的方法
    JS注册事件
    JS总结
    JS 扩展方法prototype
    Codeforces 460D Little Victor and Set(看题解)
    Codeforces 891C Envy
    Codeforces 251C Number Transformation
    Codeforces 490F Treeland Tour 树形dp
    Codeforces 605C Freelancer's Dreams 凸包 (看题解)
    几何模板
  • 原文地址:https://www.cnblogs.com/xuyongsky1234/p/4113572.html
Copyright © 2011-2022 走看看