如何将含有源代码的文本保存到数据库中?
我用的是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)