网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
..net中前台javascript与c#后台代.
.C#一个象棋游戏程序.
.对C# 2.0中匿名方法的怀疑分析.
.将指定网页添加到收藏夹的方法(c.
.在Visual C#中定义和使用自己的特.
.C#实现根据域名查询ip实例.
.RadioBox、CheckBox和Validating.
.c# 实现Word联接Excel的MailMerg.
.C#应用访问Microsoft SQL Server.
.Vsiaul C#如何读取注册信息.
.C#中的函数重载.
.利用C#远程存取Access数据库.
.怎样在你的应用程序中集成Google.
.C#编写发送电子邮件.
.C#2.0新的语法扩充(泛型,迭代器,.
.关于c#中的事件处理机制.
.用C#动态创建Access数据库.
.IP数据包的校验和算法C#版.
.关于C#中的DateTime类型的细节问.
.在C#中运用SQLDMO备份和恢复Micr.

把RichTextBox中的文本保存到Sql Server中(C#)

发表日期:2006-12-8


private void save()
  {
   FileStream stream = null;  
   SqlConnection conn = null;  
   SqlCommand cmd = null;  
   try  
   {   
    richTextBox1.SaveFile( "temp.rtf" );   
    stream = new FileStream("temp.rtf", FileMode.Open, FileAccess.Read);  
    int size = Convert.ToInt32(stream.Length);   
    Byte[] rtf = new Byte[size];   
    stream.Read(rtf, 0, size);
    conn = new SqlConnection("Database=Northwind;Integrated Security=true;");   
    conn.Open();   
    cmd = new SqlCommand("UPDATE Employees SET Photo=@Photo WHERE EmployeeID=1", conn);
    SqlParameter paramRTF =      new SqlParameter("@Photo",     SqlDbType.Image,     rtf.Length,     ParameterDirection.Input,     false,     0,0,null,     DataRowVersion.Current,     rtf);   
    cmd.Parameters.Add(paramRTF);        int rowsUpdated = Convert.ToInt32(cmd.ExecuteNonQuery());      
    MessageBox.Show(String.Format("{0} rows updated", rowsUpdated)); 
   }  
   catch(Exception ex)   {    MessageBox.Show(ex.Message);   } 
   finally  
   {   
    if ( stream != null ) stream.Close();   
    if (cmd != null ) cmd.Parameters.Clear();   
    if (conn != null) conn.Close();  
   } 
  }

  private void Get()
  {
   richTextBox1.Clear();

   SqlConnection cn     = null;   SqlCommand cmd       = null;  
   SqlDataReader reader = null;  
   try  
   {   
    cn = new SqlConnection("Database=Northwind;Integrated Security=true;");   
    cn.Open();    cmd = new SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID=1", cn);   
    reader = cmd.ExecuteReader();    reader.Read();   
    if (reader.HasRows)   
    {   
     if (!reader.IsDBNull(0))   
     {     
      Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];     
      long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length);      
      ASCIIEncoding encoding = new ASCIIEncoding();     
      richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));    
     }   
    } 
   }  
   catch(Exception ex)   {    MessageBox.Show(ex.Message);   }  
   finally  
   {   
    if (reader != null ) reader.Close();  
    if (cn != null ) cn.Close();  
   }

  }

上一篇:C# 操作文件 人气:7026
下一篇:手写分页函数C# 人气:6108
浏览全部C#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐