C#初试[C#访问SQLServer]

由于工作需要,计划拿出一个星期的时间学习一下ASP.NET。
C#初步给我的感觉,非常像java,但是写总感觉没有灵巧,也许是不太习惯的原因。先熟悉一下 再说。。。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

namespace TestCNo
{
    class Program
    {
        public const string connString = "Data Source=IP;Initial Catalog=数据库名称;User ID=用户名 ;Password=密码";

        static void Main(string[] args)
        {
            p(
"d");
            Program m = new Program();
            m.TestDataReader();
            
//m.TestDataInsert();
          
        }

        public void TestDataInsert()
        {
            string Sql =
"INSERT INTO guestbook(ID,UserName)values(newid(),'lunzi')";

            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(Sql,conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
        public void TestDataReader()
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string cmdText =
"SELECT * FROM guestbook";
                SqlCommand cmd = new SqlCommand(cmdText, conn);
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                
// Not the best way to obtain data binding ...
                Log log;
                while (reader.Read())
                {
                    
//int count = reader.FieldCount;
                    log = new Log();
                    log.setUserId(reader.GetString(1));
                    p(log.getUserId());
                    p(
"\n-------------------------");
                }
                reader.Close();
                cmd.Connection.Close();
            }
        }

        static void p(string s)
        {
            Console.WriteLine(s);
        }
    }

    class Log
    {
        private string userId;

        public void setUserId(string userid)
        {
            userId = userid;
        }

        public string getUserId()
        {
            return userId;
        }
    }
}

lunzi   2007-12-09 17:29:49 评论:0   阅读:632   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog