做记事本一般用RICHTEXTBOX控件方便一些
先拖放一个RichTextBox控件到窗体上.
我写了几个方法如下,你要完整的程序我可以发你邮箱或者QQ传你
private string read(string path)
{
FileStream fin = new FileStream(path,FileMode.Open,FileAccess.Read);
StreamReader brin = new StreamReader(fin,Encoding.Default);
string s = brin.ReadToEnd();
brin.Close();
return s;
}
private string oprater(string s)
{
return s =s+"\r\n"+ textBox1.Text +"\r\n"+ DateTime.Now.ToString();
}
private void save(string s,string path)
{
FileStream fout = new FileStream(path,FileMode.Create,FileAccess.Write);
StreamWriter brout = new StreamWriter(fout,Encoding.Default);
brout.Write(s);
brout.Close();
}







/*测试数据表:test3        Create Table test3(ID bigint not null,Files Image
null)
        */
        private void button1_Click(object
sender, EventArgs e)
        {
            FileStream fs =
File.OpenRead(this.textBox1.Text.Trim());
            byte[] bytes =
new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);

            SqlConnection con = new SqlConnection(ConStr);
       
    SqlCommand cmd = new SqlCommand("", con);
           
cmd.CommandText = "Insert into tuzhi
values('4','B8814','中冷器',@files,'2009-11-10')";
           
SqlParameter paraFile = new SqlParameter("@files", SqlDbType.Image);

            paraFile.Value = bytes;
           
cmd.Parameters.Add(paraFile);
            this.Cursor =
Cursors.WaitCursor;
            con.Open();
            int i =
cmd.ExecuteNonQuery();
            MessageBox.Show("成功");
   
        con.Close();
            this.Cursor = Cursors.Default;

        }

        private void button2_Click(object sender,
EventArgs e)
        {
            byte[] mypdffile = null;

            SqlConnection con = new SqlConnection(ConStr);
       
    SqlCommand cmd = new SqlCommand("", con);
           
cmd.CommandText = "Select tu From tuzhi Where ID='1'";
           
con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
               
mypdffile = (byte[])dr.GetValue(0);
            }
           
con.Close();
            this.Cursor = Cursors.WaitCursor;
   
        try
            {
                FileStream fs = new
FileStream("D:\\aa1.pdf", FileMode.Create);
               
fs.Write(mypdffile, 0, mypdffile.Length);
               
fs.Flush();
                fs.Close();
               
this.webBrowser1.Navigate("D:\\aa1.pdf");
            }
       
    catch { }
            this.Cursor = Cursors.Default;
     
  }

        private void Form1_Load(object sender, EventArgs
e)
        {
            //连接字符串自己改下,这是我的测试数据库的链接字符串
   
        ConStr = "server=192.168.0.2;database=drawing 
;uid=sa;Pwd=7612098;";
        }
posted on 2010-01-12 16:12  superlee  阅读(1187)  评论(0编辑  收藏  举报