hey i m new to C#.. if want 2 store word document in Access in C# ???? How it can be done????????
PLz help me i have to submit my assignment but i m stuck with this point all my assignment is ready so plz help me as soon as possible thanx
Mitja Bonca 557 Nearly a Posting Maven
try this out:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
//p.InsertDoc();
//p.RetriveDoc();
Console.Read();
}
public void InsertDoc()
{
byte[] doc = GetDocFromHDD(@"C:\Users\Radwan\Documents\Test.doc");
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; User Id=;Data Source=C:\\Users\\Radwan\\Documents\\Database1.mdb");
OleDbCommand addDoc = new OleDbCommand("insert into table1 (data,fileLength) values(@doc,@length)", Conn);
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter();
MyDataAdapter.InsertCommand = addDoc;
OleDbParameter docPar = new OleDbParameter("@data", OleDbType.VarBinary, doc.Length);
docPar.Value = doc;
OleDbParameter length = new OleDbParameter("@length", OleDbType.Integer);
length.Value = doc.Length;
MyDataAdapter.InsertCommand.Parameters.Add(docPar);
MyDataAdapter.InsertCommand.Parameters.Add(length);
// connect
Conn.Open();
// insert
addDoc.ExecuteNonQuery();
Conn.Close();
//........
}
public void RetriveDoc()
{
//byte[] doc = GetDoc(@"C:\Users\Radwan\Documents\Test.doc");
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; User Id=;Data Source=C:\\Users\\Radwan\\Documents\\Database1.mdb");
OleDbCommand selectDoc = new OleDbCommand("select data,FileLength from table1 where id = @id", Conn);
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter();
MyDataAdapter.SelectCommand = selectDoc;
OleDbParameter id = new OleDbParameter("@id", OleDbType.Integer);
id.Value = 1;
MyDataAdapter.SelectCommand.Parameters.Add(id);
// connect
Conn.Open();
// insert
OleDbDataReader reader = selectDoc.ExecuteReader();
byte[] buffer = null;
while (reader.Read())
{
int docLength = reader.GetInt32(1);
buffer = new byte[docLength];
reader.GetBytes(0, 0, buffer, 0, docLength);
string s = reader.GetDataTypeName(0);
//reader.
}
Conn.Close();
SaveDocToHDD(buffer);
//........
}
private void SaveDocToHDD(byte[] buffer)
{
FileStream fs = new FileStream(@"C:\Users\Radwan\Documents\Output.doc", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buffer);
}
private byte[] GetDocFromHDD(string p)
{
FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
return br.ReadBytes((int)fs.Length);
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.