C#编程访问数据库测试程序
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CSharpBegining
{
class Program
{
//C#编程访问数据库测试程序
static void Main(string[] args)
{
adpter();
//this.BaseCommand();
}
private static void adpter()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CSharpBegining.Properties.Settings.ArchivesConnectionString"].ConnectionString);
//conn.Open();
}
private void BaseCommand()
{
SqlConnection conn = new SqlConnection();
//conn.ConnectionString = ConfigurationManager.ConnectionStrings["CSharpBegining.Properties.Settings.ArchivesConnectionString"].ConnectionString;
conn.ConnectionString = "Data Source=wei-pc;Initial Catalog=Archives;Integrated Security=true";
//exec ExecuteReader()
SqlCommand cmdReader = new SqlCommand();
cmdReader.CommandText = "select * from [Transfer]";
cmdReader.Connection = conn;
//exec ExecuteScalar()
//SQL SERVER 不区分大小写,但是个人感觉还是区分比较好,以免出现莫名其妙的问题
SqlCommand cmdCount = new SqlCommand("select count(*) from [user]", conn);
//exec ExecuteNonQuery
SqlCommand cmdNonQ = new SqlCommand("insert into [User] values (007,'fireice','50126',2009/4/1,10)", conn);
//测试CommandType.TableDirect。结果失败。SQL SERVER 2005版本为开发版
//SqlCommand cmdType = new SqlCommand("[User]", conn);
//.Net Framework SqlClient 数据提供程序不支持ommandType 枚举值 512
//cmdType.CommandType = CommandType.TableDirect;
string tableName = "User";
SqlCommand cmdPrt = new SqlCommand("SELECT * FROM [" + tableName + "] WHERE userID=@userID", conn);
cmdPrt.Parameters.Add("@userID", SqlDbType.NVarChar).Value = "7";
////SqlCommand cmdPrt = new SqlCommand("SELECT * FROM [@User] WHERE userID=@userID", conn);
//这种方法行不通,存储过程中也尚未找到解决表名作为参数的办法
////cmdPrt.Parameters.Add("@User", SqlDbType.Structured).Value = "User";
try
{
conn.Open();
//int rowCount = (int)cmdCount.ExecuteScalar();
//int res = cmdNonQ.ExecuteNonQuery();
SqlDataReader rd;
//rd= cmdReader.ExecuteReader();
//rd = cmdType.ExecuteReader();
rd = cmdPrt.ExecuteReader();
while (rd.Read())
{
System.Console.Write(rd[0].ToString() + " ");
System.Console.Write(rd.GetString(1) + " ");
System.Console.Write(rd[2] + " ");
System.Console.Write(rd["loginTimes"] + " ");
System.Console.Write(rd.FieldCount + " "); //列数量
System.Console.WriteLine();
System.Console.WriteLine();
for (int index = 0; index < rd.FieldCount; index++)
{
System.Console.Write(rd[index] + "_");
}
System.Console.WriteLine("\n");
}
}
catch (System.Exception e)
{
System.Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CSharpBegining
{
class Program
{
//C#编程访问数据库测试程序
static void Main(string[] args)
{
adpter();
//this.BaseCommand();
}
private static void adpter()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CSharpBegining.Properties.Settings.ArchivesConnectionString"].ConnectionString);
//conn.Open();
}
private void BaseCommand()
{
SqlConnection conn = new SqlConnection();
//conn.ConnectionString = ConfigurationManager.ConnectionStrings["CSharpBegining.Properties.Settings.ArchivesConnectionString"].ConnectionString;
conn.ConnectionString = "Data Source=wei-pc;Initial Catalog=Archives;Integrated Security=true";
//exec ExecuteReader()
SqlCommand cmdReader = new SqlCommand();
cmdReader.CommandText = "select * from [Transfer]";
cmdReader.Connection = conn;
//exec ExecuteScalar()
//SQL SERVER 不区分大小写,但是个人感觉还是区分比较好,以免出现莫名其妙的问题
SqlCommand cmdCount = new SqlCommand("select count(*) from [user]", conn);
//exec ExecuteNonQuery
SqlCommand cmdNonQ = new SqlCommand("insert into [User] values (007,'fireice','50126',2009/4/1,10)", conn);
//测试CommandType.TableDirect。结果失败。SQL SERVER 2005版本为开发版
//SqlCommand cmdType = new SqlCommand("[User]", conn);
//.Net Framework SqlClient 数据提供程序不支持ommandType 枚举值 512
//cmdType.CommandType = CommandType.TableDirect;
string tableName = "User";
SqlCommand cmdPrt = new SqlCommand("SELECT * FROM [" + tableName + "] WHERE userID=@userID", conn);
cmdPrt.Parameters.Add("@userID", SqlDbType.NVarChar).Value = "7";
////SqlCommand cmdPrt = new SqlCommand("SELECT * FROM [@User] WHERE userID=@userID", conn);
//这种方法行不通,存储过程中也尚未找到解决表名作为参数的办法
////cmdPrt.Parameters.Add("@User", SqlDbType.Structured).Value = "User";
try
{
conn.Open();
//int rowCount = (int)cmdCount.ExecuteScalar();
//int res = cmdNonQ.ExecuteNonQuery();
SqlDataReader rd;
//rd= cmdReader.ExecuteReader();
//rd = cmdType.ExecuteReader();
rd = cmdPrt.ExecuteReader();
while (rd.Read())
{
System.Console.Write(rd[0].ToString() + " ");
System.Console.Write(rd.GetString(1) + " ");
System.Console.Write(rd[2] + " ");
System.Console.Write(rd["loginTimes"] + " ");
System.Console.Write(rd.FieldCount + " "); //列数量
System.Console.WriteLine();
System.Console.WriteLine();
for (int index = 0; index < rd.FieldCount; index++)
{
System.Console.Write(rd[index] + "_");
}
System.Console.WriteLine("\n");
}
}
catch (System.Exception e)
{
System.Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
}
}