将int变量保存到数据库并检索它 [英] save a variable of int to database and retrive this

查看:99
本文介绍了将int变量保存到数据库并检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个整数保存到数据库中并重试这个



怎么做?

i want save a integer into database and retrive this

how to do this ?

推荐答案

你有 Google [ ^ ],但是?


保存它:

Save it:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn) VALUES (@MYCOL)", con))
        {
        com.Parameters.AddWithValue("@MYCOL", myIntegerValue);
        com.ExecuteNonQuery();
        }
    }



检索它:


Retrieve it:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT myColumn FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int myInt = (int) reader["myColumn"];
                Console.WriteLine("ID: {0}", myInt);
                }
            }
        }
    }


这篇关于将int变量保存到数据库并检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆