如何将含有源代码的文本保存到数据库中?

jonsonzxw 2003-01-08 09:25:22
我用的是Access2000数据库,有一字段content为备注型,我想用来保存一些asp.net的技术文章,但总是不能保存成功,多次尝试才发现是由文章中的一些源代码造成的,请问如何才能将其保存进去呢?

附:文章内容片断
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
Database terminology
Structured Query Language (SQL)
back to the top
How to Run a Command
Commands are issued against databases to take actions against data stores and to include any statement that can be issued against a database. You can use the OleDbCommand or the SqlCommand classes to get a command to your data store, and OleDbCommand can be specific to the data store. This article demonstrates both the SqlClient class (to connect to a computer that is running Microsoft SQL Server) and the OleDb class (for any database that has an OLE DB or ODBC driver available) within ADO.NET. However, the code is generally the same for both.

With ADO, you can issue commands through the Command, the Connection, or the Recordset object. In ADO.NET, only the Command objects (SqlCommand or OleDbCommand) run commands.

To run a command, follow these steps:
Follow these steps to create a new console application in Visual Basic .NET:
Start Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Console Application under Templates.
Make sure that your project contains a reference to the System.Data namespace, and add a reference if it does not.
Use the Imports statement on the System and System.Data namespaces so that you do not have to qualify declarations in those namespaces later in your code. You can also include System.Data.SqlClient or System.Data.OleDb, depending on which one you are using.
Imports System
Imports System.Data
Imports System.Data.SqlClient

Before you can create a connection to a database, you must have a connection string. Connection strings contain all of the information that you need to establish a database connection, including the server name, the database name, the user ID, and the password. For example, the following connection string points to a local computer that is running SQL Server with a blank password for the 'sa' account:

For OleDb connections:
Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=pubs;Data Source=(local)

For SqlClient connections:
User ID=sa;Initial Catalog=pubs;Data Source=(local)

NOTE: If you need more assistance determining the connection string for your database, search for "ConnectionString" in the Microsoft Developer Network (MSDN) Library at the following Microsoft Web site:

http://search.microsoft.com/us/dev/default.asp


Visual Studio creates a module and an empty Main() procedure. Declare a string variable, and store the appropriate connection string for your database in this procedure:
Sub Main() // 好象是这里会出错
Dim sConnectionString As String _
= "User ID=sa;Initial Catalog=pubs;Data Source=(local)"
End Sub
...

Create a SqlCommand or OleDbCommand object, and pass in the command that you want to run and the connection object that you created in the previous step. The following sample code passes in the INSERT statement:
Dim sSQL As String = "INSERT INTO Employee " & _
"(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date)" & _
"VALUES ('MSD12923F', 'Duncan', 'W', 'Mackenzie', " & _
"10, 82,'0877','2001-01-01')" // 好象这里也会出错
Dim objCmd As New SqlCommand(sSQL, objConn)
...全文
100 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
一并问了,如何将其转换回来并加以显示呢?
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
可能文章很长(几页甚至几十页),那我要声明多大的字节数组才合适呢?
ssdjmcj8048 2003-01-08
  • 打赏
  • 举报
回复
设表有两个字段name(text),data(oledb)
this.oleDbConnection1.Open();
OleDbCommand cmd = this.oleDbConnection1.CreateCommand();
cmd.CommandText = "INSERT INTO test (name,data) VALUES(?,?)";
OleDbParameter p = cmd.Parameters.Add("name", OleDbType.VarChar);
p.Value = "wh";
byte[] yourData = new Byte[100];
p = cmd.Parameters.Add("data", OleDbType.Binary);
p.Value = yourData;
try{
cmd.ExecuteNonQuery();
}
catch(Exception ep){
MessageBox.Show(ep.Message);
}
this.oleDbConnection1.Close();
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
我还是都试一下吧
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
把每个单引号用两个单引号代替.

好象可以正确保存了,但还有什么符号必须要转换的呢,就怕以后又会遇到其他情况,敬请指点!
project 2003-01-08
  • 打赏
  • 举报
回复
那就只能先把文件转成byte之类的数据类型,然后再把byte转成string存起来不就没有源码吗?如果是大型数据库用binary就比较方便了。
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
我是将整个文章作为一个字符串保存到一个备注型的字段中去的,刚才按sqhua(拉长弓兮射天狼)的意思测了一下,确实是单引号的问题,是不是必须要将该备注型字段改成“OLE对象” 字段,再通过ssdjmcj8048(不爱我的我不爱) 给的思路去保存呢,然而如何将其转换回来并加以显示呢?
chinchy 2003-01-08
  • 打赏
  • 举报
回复
把每个单引号用两个单引号代替.
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
你是说将备注型字段改为“OLE对象” 字段,那该如何保存呢,有无例子?谢谢啦!
ssdjmcj8048 2003-01-08
  • 打赏
  • 举报
回复
不知道你是通过什么样的方式去存的。
可能使用普通参数方式(varchar或text类型)是不行的。应该使用byte[]的参数类型即大对象,在Access数据库中为Ole对象。
具体是这样的:在Command中的参数设置为Byte[]类型,然后将这段文本转换为Byte[]作为参数传进去,再执行就可以成功了,当然以后用时也要在转换回来。
其实如果在SqlServer中,有text类型的字段就可以直接搞定
sqhua 2003-01-08
  • 打赏
  • 举报
回复
你是直接生成SQL语句来保存的吧,此时有单引号就不行了

你可以把这些文本当成二进制数据存入 “OLE对象” 字段中
jonsonzxw 2003-01-08
  • 打赏
  • 举报
回复
技术文章难免会有源代码,怎么办呢,网上那些文章管理系统是不是都用文件而不用库保存的?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧