📄 filedb.cs
字号:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Example_11_1
{
/// <summary>
/// Summary description for FileDB.
/// </summary>
public class FileDB
{
public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public SqlDataReader GetFiles()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetFiles",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
return(dr);
}
public int AddFile(String sFileName,String sFileUrl,String sFileKind)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddFile",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterFileName = new SqlParameter("@FileName",SqlDbType.VarChar,100);
parameterFileName.Value = sFileName;
myCommand.Parameters.Add(parameterFileName);
SqlParameter parameterFileUrl = new SqlParameter("@FileUrl",SqlDbType.VarChar,200);
parameterFileUrl.Value = sFileUrl;
myCommand.Parameters.Add(parameterFileUrl);
SqlParameter parameterFileKind = new SqlParameter("@FileKind",SqlDbType.VarChar,100);
parameterFileKind.Value = sFileKind;
myCommand.Parameters.Add(parameterFileKind);
SqlParameter parameterFileID = new SqlParameter("@FileID",SqlDbType.Int,4);
parameterFileID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterFileID);
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
return (int)parameterFileID.Value;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -