1 // 对存储过程的分析:实例
2 // string G_name ,string G_password为传递给此存储过程的参数,string Loging表示方法名和类型
3
4 public string Login( string G_Name , string G_Passord)
5 {
6
7 SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppeSettings[ " ConnectionString " ]);
8 // 此为连接语句
9 /**/ /*
10 ConfigurationSettings.AppeSettings["ConnectionString"]
11 表示从Configurantion加载了一条连接语句
12 Config里的语句为
13 <appSettings>
14 <add key="ConnectionString" value="server=Localhost;uid=sa;pwd=sa; dataBase= Global" />
15 </appSettings>
16 <system.web>
17 注意此标是放在<system.web>的上面
18 */
19 SqlCommand Comm = new SqlCommand( " Login " ,Conn);
20 // 新建一个SqlCommand的实例Comm并把它标记为储蓄过程名为Login。
21
22 Comm.CommandType = CommandType.StoredProcedure;
23 // 将Comm标记为储蓄过程
24
25
26
27 // 下面为存储过程添加参数
28 SqlParameter parameterG_name = new SqlParameter( " @G_name " ,SqlDbType.NVarChar, 20 );
29 // 新建了一个SqlParameter的储蓄过程参数实例:实例名为pranmeterG_name
30 // 并定义了一个名为"@G_name"的参数名,定义类型SqlDbType为NvarChar 字节数为20;
31
32 parameterG_name.Value = G_Name;
33 // 为参数实例parameterG_name赋值 为这个值是从方法Login中传递进来的值
34 // 注意parameterG_name.Value=G_name 中的G_name和语句new SqlParameter("@G_name",SqlDbType.NVarChar,20);
35 // 中的@G_name是不同,@G_name是为一个SqlParameter的储蓄过程定义的一个参数名
36
37 Comm.Parameters.Add(prarameterG_name);
38 // 为Comm为添加参数paratemterG_name
39
40
41
42
43 SqlParameter parameterG_password = new SqlParameter( " @G_password " ,SqlDbType.NVarChar, 20 );
44 parameterG_password.Value = G_password;
45 Comm.Parameters.Add(parameterG_password);
46
47 SqlParameter parameterG_Id = new SqlParameter( " @G_Id " ,SqlDbType.Int, 4 );
48 parameterG_Id.Direction = ParameterDirection.Output;
49 // parameterG_Id.dDirection 获取或者设置一个值,该值指示指示参数是只可
50 // 只可以输入,只可以输出,双向 还是存储过程返回值参数
51 // ParameterDirection.Output;定义了此参数为输出参数
52 Comm.Parameters.Add(paramerG_Id);
53
54 // 打开连接并执行Command命令
55 Conn.Open();
56 Comm.ExecuteNonQuery();
57 Conn.Close();
58
59 // 对获得parameterG_Id.Value的值进行处理
60 int G_id = ( int )(parameterG_Id.Value);
61
62 if (G_id == 0 )
63 {
64 return null ;
65 // 返回空
66 }
67 else
68 {
69 G_id.ToString();
70 // 将此信息转换为等效字符串的表现形势
71 }
posted @ 2006-10-30 20:05 剑落飘香 阅读(289) 评论(0)
编辑
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlcon =new SqlConnection("Data Source=.;database=chapter;user id=sa;pwd=;");
SqlCommand sqlcom = new SqlCommand("select * from employee", sqlcon);
sqlcom.CommandTimeout = 30;
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlcom);
sqlcon.Open();
DataSet mySet = new DataSet();
myAdapter.Fill(mySet, "employees");
/*建立dataTable*/
DataTable employee = new DataTable("employee");
/*添加datacolumn对象来定义dataTable的构架*/
DataColumn code = new DataColumn("code");
code.DataType = typeof(Int32) ;
DataColumn name = new DataColumn("name");
name.DataType = typeof(string);
name.MaxLength = 30;
DataColumn sex = new DataColumn("sex");
sex.DataType = typeof(string);
sex.MaxLength = 2;
/*将定义的三个datacolumn对象添加到dataTable中*/
employee.Columns.Add(code);
employee.Columns.Add(name);
employee.Columns.Add(sex);
/*要向DataTable中添加新行,必须先用NewRow方法来返回新的DataRow对象,
NewRow方法返回具有dataTable的架构的行*/
DataRow newemployee = employee.NewRow();
newemployee["code"] = "98";
newemployee["name"] = "清风";
newemployee["sex"] = "男";
employee.Rows.Add(newemployee);
Console.WriteLine("employee");
Console.WriteLine("code:{0},name:{1},sex:{2}", employee.Rows[0]["code"].ToString(), employee.Rows[0]["name"].ToString(), employee.Rows[0]["sex"]);
}
}
}
posted @ 2006-10-30 20:03 剑落飘香 阅读(450) 评论(0)
编辑