如何用olesdbcommand对象读取数据库记录

如何用olesdbcommand对象读取数据库记录,第1张

DB.setDBPath(Page.Server.MapPath("/App_Data/TestModel.accdb"))

OleDbConnection myconn = DB.createConnection()

myconn.Open()

string selectStr = "select * from UserInfo"

OleDbDataAdapter oda = new OleDbDataAdapter(selectStr, myconn)

DataSet ds = new DataSet()

oda.Fill(ds)

GV.DataSource = ds

GV.DataBind()

myconn.Close()

public static DataSet GetDataSet(string FilePath)

{

string OledbConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0Data Source='" + FilePath + "'Extended Properties='Excel 8.0HDR=YesIMEX=1'")

OleDbConnection conn = new OleDbConnection(OledbConnectionString)

ArrayList SheetNameList = new ArrayList()

try

{

if (conn.State == ConnectionState.Closed)

{

conn.Open()

}

DataTable dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" })

string SheetName = ""

for (int i = 0i <dtExcelSchema.Rows.Counti++)

{

SheetName = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString()

SheetNameList.Add(SheetName)

}

}

catch (Exception ex)

{

throw ex

}

finally

{

conn.Close()

}

DataSet dsExcel = new DataSet()       try

{

string strSql = ""           for (int i = 0i <SheetNameList.Counti++)

{

strSql = "select * from [" + (string)SheetNameList[i] + "]"

OleDbDataAdapter oleExcelDataAdapter = new OleDbDataAdapter(strSql, conn)

DataTable dtExcel = new DataTable((string)SheetNameList[i])

oleExcelDataAdapter.Fill(dtExcel)

dsExcel.Tables.Add(dtExcel)

}

return dsExcel

}

catch (Exception ex)

{

throw ex

}

}这个方法就是从EXCEL文件读取数据转换为DataSet 下面一段时调用的时候要注意的,写要在服务器端保存一下上传的EXCEL,然后再调用        string filePath = ""

DataSet ds = new DataSet()

if (System.IO.Path.GetExtension(FileUpload1.FileName) != ".xls")

{

ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alter('hao')</script>")

return

}

else

{

filePath = "D:\\" + FileUpload1.FileName

FileUpload1.SaveAs(filePath)

string fileName = FileUpload1.FileName

int start = fileName.IndexOf('.')

fileName = fileName.Substring(0, start)

ds = GetDataSet(filePath)

}

(一)把文件内容写入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)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6713969.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存