不知道你是不是要数据库访问的代码:
1:using指令:
using System.Data.SqlClient; 这是访问SQL Server数据库 的类;
2:组织一下C#代码
3:静态的SQL连接
public static SqlConnection cnn;
public const string
ConnectionString = @"data source=localhost;
initial catalog=......; integrated security=SSPI;
persist security info=False;packet size=4096"
public const string ConnectionString2 = @"data source =localhost;
initial catalog=......;persist security info=False;user id=sa;packet size=4096";
4,定义DataAdapter, parameter,DataSet和Dataview
public static SqlDataAdapter da;
public static SqlParameter param;
public static DataSet da =new DataSet();
public static DataVIew dv=new DataView();
5,排序字符串
public static string Sort;
6,Option表
public static string PrintPicture;
7,构造函数
8,SQL连接
cnn=new SqlConnection(); 添加在initializeComponent方法调用的后面;
9,设置DataView属性
dv. AlllowNew=true;
dv.AllowEdit=true;
dv.AllowDelete=true;
10,Clobal_Load方法
private void Global_Load(object sender,System.EventArgs e)
{
}
11,定义循环变量
int i;
12,打开数据库连接
try
{
cnn.ConnectionString =ConnectionString;
cnn.Open();
}
catch
{
try
{
cnn.ConnectionString=Connnection2;
cnn.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
13,创建SQL命令对象
SqlCommand cmd =cnn.CreateCommand();
14,查询MSDE数据库的数据
cmd.CommandText="SELECT * FROM tbl";
da=new SqlDataAdapter(cmd);
da.FillSchema(ds,SchemaType.Source);
da.AcceptChangesDuringFill=true;
da.Fill(ds,"tbl");
dv.Table=ds.Tables["tbl"];
15,读取排序次序
string[] sortValue=new String[2];
string[] sortOrder=new String[2];
cmd.CommandText="SELECT * FROM tbl";
SqlDataReader drSort=cmd.ExecuteReader();
i=0;
while(drSort.Reader())
{
sortValue[i] =drSort.GetString(2);
sortOrder[i]=drSort.GetString(2);
++i;
}
drSort.Close();
Array.Sort(sortOrder,sortValue);
Sort="";
for(i=0;i<2;i++)
{
case "0":
break;
default:
Sort+=sortValue[i];
Sort+=",";
break;
}
}
Sort+="Key";
dv.Sort=Sort;
16读取选项文件
cmd.CommandText="SELECT * FROM tbl";
SqlDataReader drOption=smd.ExcuteReader();
while(drOprion.Read())
{
switch(drOption.Gerstring())
{
case "PrintPicture":
PrintPicture=drOprion.Gerstring(2);
switch(PrintPictures)
{
case"Yes":
mnuPrintWithPicture.Checked=true;
break;
case "No":
mnuPrintWithoue\tPicture.Checked=true;
}
break;
}
}
17,为Sort和Option表添加默认数据
INSERT INTO TBLSort(......)
VALUES(.......)
18备份项目
19验证可以查看数据