作者:itlzk | 来源:互联网 | 2023-08-23 09:12
C#连接数据库sqlserver2005,执行存储过程的实例:[csharp]viewplaincopyusingSystem;usingSystem.Collections.Generi
C#连接数据库sqlserver2005,执行存储过程的实例:
[csharp] view plaincopy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using System.Data;
- using System.Data.SqlClient;
-
- namespace DBdemo3
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- SqlConnection cnn = new SqlConnection();
- cnn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mapdemo;Data Source=PC-20130306BGML";
-
- cnn.Open();
-
-
- SqlParameter prm;
-
-
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = cnn;
-
-
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = "InsStuProc";
-
-
- prm = new SqlParameter();
- prm.ParameterName = "@Sno";
- prm.SqlDbType = SqlDbType.Char;
- prm.Size = 6;
- prm.Value = "060010";
- prm.Direction = ParameterDirection.Input;
- cmd.Parameters.Add(prm);
-
- prm = new SqlParameter();
- prm.ParameterName = "@SName";
- prm.SqlDbType = SqlDbType.VarChar;
- prm.Size = 10;
- prm.Value = "张三";
- prm.Direction = ParameterDirection.Input;
- cmd.Parameters.Add(prm);
-
- prm = new SqlParameter();
- prm.ParameterName = "@Age";
- prm.SqlDbType = SqlDbType.TinyInt;
- prm.Value = 20;
- prm.Direction = ParameterDirection.Input;
- cmd.Parameters.Add(prm);
-
-
- cmd.ExecuteNonQuery();
-
-
- }
- }
- }