C# 从 ASP.NET 插入到 MS Access

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/459442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:13:19  来源:igfitidea点击:

INSERT from ASP.NET to MS Access

c#asp.netsql-servervisual-studioms-access

提问by

We are trying to build a Help Desk ticketing system just for intranet. Deciding upon the ASP .NET (C#) with Visual Studio 2008 Express (think we have a full version floating around if we need it). Nothing fancy, couple of pages grabbing NTLM information, system information and storing it along with their problem in a database. Goal is to make it simple, but instead of using our SQL Server 2000 back end, the admin wants me to use MS Access. I have the GridView and connections running smooth. Can pull select queries until my heart is content. However, tying in a couple variables with a text box on a submit button into say an INSERT statement.. well I don't even know where to begin with MS Access. Every internet example is in VB .NET plus seems to be hand coding what Visual Studio has already done for me in a few clicks.

我们正在尝试为 Intranet 构建一个 Help Desk 票务系统。决定使用 Visual Studio 2008 Express 的 ASP .NET (C#)(如果我们需要的话,我们有一个完整的版本)。没什么特别的,几页抓取 NTLM 信息、系统信息并将其与他们的问题一起存储在数据库中。目标是让它变得简单,但管理员希望我使用 MS Access,而不是使用我们的 SQL Server 2000 后端。我的 GridView 和连接运行顺畅。可以拉选择查询,直到我心满意足。但是,将几个变量与提交按钮上的文本框绑定到一个 INSERT 语句中......我什至不知道从哪里开始使用 MS Access。每个 Internet 示例都在 VB 中。NET plus 似乎是手动编码 Visual Studio 已经通过几次点击为我完成的工作。

Is MS Access going to be too hard for all we want to do? If not, where do we begin to simply submit this data into the tables?

对于我们想做的所有事情,MS Access 是否会太难?如果没有,我们从哪里开始简单地将这些数据提交到表中?

Edit: After a bunch of playing around we have the OleDB working. It's not pretty, yes SQL Server would be awesome but, sometimes you just have to play ball.

编辑:经过一番尝试后,我们让 OleDB 正常工作。它并不漂亮,是的,SQL Server 会很棒,但是,有时您只需要打球。

Edit: Anyone looking for an actual coded answer, here you are. There has got to be others out there in the same boat.

编辑:任何正在寻找实际编码答案的人,都在这里。必须有其他人在同一条船上。

    string userIP = Request.UserHostAddress.ToString();
    string userDNS = Request.UserHostName.ToString();
    string duser = Request.ServerVariables["LOGON_USER"];  //NTLM Domain\Username
    string computer = System.Environment.MachineName.ToString(); //Computer Name
    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\helpdesk.MDB;";

    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "INSERT INTO ticketing ([user], [comp], [issue]) VALUES (@duser, @computer, @col3)";
    cmd.Parameters.Add("@duser", OleDbType.VarChar).Value = duser;
    cmd.Parameters.Add("@computer", OleDbType.VarChar).Value = computer;
    cmd.Parameters.Add("@col3", OleDbType.LongVarChar).Value = TextBox1.Text;
    cmd.ExecuteNonQuery();
    conn.Close();

采纳答案by Frederik Gheysels

I also suggest using SQL Server, but considering your problem: What is your problem writing an INSERT query for Access ? You should make use of the classes that you'll find in the System.Data.OleDb namespace:

我还建议使用 SQL Server,但考虑到您的问题:为 Access 编写 INSERT 查询有什么问题?您应该使用在 System.Data.OleDb 命名空间中找到的类:

  • OleDbConnection
  • OleDbCommand
  • 数据库连接
  • 数据库命令

Quick'n dirty code (not compiled whatsoever):

Quick'n 脏代码(未编译):

OleDbConnection conn = new OleDbConnection (connectionString);

OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText= "INSERT INTO myTable (col1, col2) VALUES (@p_col1, @p_col2)";
command.Parameters.Add ("@p_col1", OleDbType.String).Value = textBox1.Text;
...
command.ExecuteNonQUery();

There are some caveats with the OleDb classes however (like adding the Parameters to the collection in the order that they occur in your SQL statement, for instance).

但是,OleDb 类有一些注意事项(例如,将参数按照它们在 SQL 语句中出现的顺序添加到集合中)。

回答by Joel Coehoorn

The admin is nuts. Access is an in-processdatabase, and as such is not well suited for web sites where users will be creating or updating records.

管理员疯了。Access 是一个in-process数据库,因此不太适合用户将创建或更新记录的网站。

But as far as creating INSERT queries go, Access is no harder than anything else. If you can't create INSERT queries for Access you'll probably have trouble with SQL Server as well.

但就创建 INSERT 查询而言,Access 并不比其他任何事情都难。如果您不能为 Access 创建 INSERT 查询,则 SQL Server 也可能会遇到问题。

回答by dan

Don't bother with Access. Use SQL Server Express. There's also an admin tool for it that looks like the full blown SQL Server management tool.

不要打扰访问。使用 SQL Server Express。它还有一个管理工具,看起来像成熟的 SQL Server 管理工具。

回答by winwaed

Access has its place, and can usually do more than what most people give it credit for, but yes you want to use SQL Server in ones of its many forms (eg. SQL Server Express) or another proper "server" database for a web app like this.

Access 有它的位置,并且通常可以做比大多数人认为的更多的事情,但是是的,您希望以多种形式(例如 SQL Server Express)中的一种使用 SQL Server 或用于 Web 的其他适当的“服务器”数据库像这样的应用程序。