代码如下:
/// <summary>/// 取所有表名
/// </summary>
/// <returns></returns>
public List<string> GetTableNameList()
{
List<string> list = new List<string>()
OleDbConnection Conn = new OleDbConnection(ConnStr)
try
{
if (Conn.State == ConnectionState.Closed)
Conn.Open()
DataTable dt = Conn.GetSchema("Tables")
foreach (DataRow row in dt.Rows)
{
if (row[3].ToString() == "TABLE")
list.Add(row[2].ToString())
}
return list
}
catch (Exception e)
{ throw e }
finally { if (Conn.State == ConnectionState.Open) Conn.Close() Conn.Dispose() }
}
/// <summary>
/// 取指定表所有字段名称
/// </summary>
/// <returns></returns>
public List<string> GetTableFieldNameList(string TableName)
{
List<string> list = new List<string>()
OleDbConnection Conn = new OleDbConnection(ConnStr)
try
{
if (Conn.State == ConnectionState.Closed)
Conn.Open()
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = "SELECT TOP 1 * FROM [" + TableName + "]"
cmd.Connection = Conn
OleDbDataReader dr = cmd.ExecuteReader()
for (int i = 0 i < dr.FieldCount i++)
{
list.Add(dr.GetName(i))
}
}
return list
}
catch (Exception e)
{ throw e }
finally
{
if (Conn.State == ConnectionState.Open)
Conn.Close()
Conn.Dispose()
}
}
(一)把文件内容写入Access数据库的OLE对象字段中:if (File.Exists(txtBrow.Text) != false) // 文本框txtBrow中内容为文件路径及文件名
{
//获取文件后缀
FileInfo p = new FileInfo(txtBrow.Text.Trim())
F_str_Type = p.Extension.ToLower()
if (F_str_Type.Length >5)
{
MessageBox.Show("不可识别的文件格式,请重新确认!","警告")
return
}
//判断文件大小
if (p.Length == 0)
{
MessageBox.Show("文件的大小为“0”,不能保存!", "警告")
return
}
//创建文件对象以打开的形式读取文件
FileStream sFileStream = new FileStream(txtBrow.Text, FileMode.Open)
//分配数组大小
byte[] bFile = new byte[sFileStream.Length]
//将文件内容读进数组
sFileStream.Read(bFile, 0, (int)sFileStream.Length)
//关闭文件对象
sFileStream.Close()
//查找文档类别号
OleDbDataReader topicread = SaveConn.GetReader("select File_ID from FileTopic where File_Topic='" + cbbTopic.Text.Trim() + "'")
//Read()方法用来读取OleDbDataReader对象中的记录
topicread.Read()
T_int_Topic=(int)topicread["File_ID"]
OleDbConnection conn = SaveConn.GetConn()
conn.Open()
OleDbCommand com = conn.CreateCommand()
com.CommandText = "insert into FileTitle(File_CodeID,File_TopicID,File_Title,File_Time,File_Save,File_Name,File_Type) values(@File_CodeID,@File_TopicID,@File_Title,@File_Time,@File_Save,@File_Name,@File_Type)"
com.Parameters.AddWithValue("@File_CodeID", txtCode.Text.Trim()) //文档编号
com.Parameters.AddWithValue("@File_TopicID", T_int_Topic) //文档类别号
com.Parameters.AddWithValue("@File_Title", txtTitle.Text.Trim()) //文档标题
com.Parameters.AddWithValue("@File_Time", txtTime.Text.Trim()) //存储时间
com.Parameters.AddWithValue("@File_Save", bFile) //文档内容
com.Parameters.AddWithValue("@File_Name", txtName.Text.Trim()) //经手人
com.Parameters.AddWithValue("@File_Type", F_str_Type)//文档类型
com.CommandType = CommandType.Text
com.ExecuteNonQuery()
conn.Close()
MessageBox.Show("文档资料添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
(二)双击DataGridView控件某一行,显示文档内容
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
string F_str_Open
//如果不存在就创建Temp文件夹
if (Directory.Exists(Application.StartupPath + @"/Temp") == false)
{
Directory.CreateDirectory(Application.StartupPath + @"/Temp")
}
try
{
OleDbDataReader sqlread = SaveConn.GetReader("select File_Save,File_Type from FileTitle where File_CodeID='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'")
sqlread.Read()
//判断文件是否已存在.
if (File.Exists(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]))
{
//如存在则删除
File.Delete(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"])
}
byte[] bFile=(byte[])sqlread[0]
//创建文件对象
System.IO.FileStream sFileStream = new System.IO.FileStream(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"], System.IO.FileMode.Create)
//将数组的内容写进文件
sFileStream.Write(bFile, 0, bFile.Length)
//关闭文件
sFileStream.Close()
Process p = new Process()
p.StartInfo.FileName = Convert.ToString(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"])
p.Start()
p.Close()
sqlread.Close()
}
catch (Exception ee)
{
MessageBox.Show(ee.Message)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)