简体   繁体   English

我无法使用Visual Studio将数据发送/插入到新数据库表中

[英]I cannot send/insert data into my new database table using Visual Studio

I am having trouble trying to send my data into my database. 我在尝试将数据发送到数据库时遇到麻烦。

Basically, I am using an ASP.Net Web form already implemented by Visual Studio 2012 (Professional). 基本上,我使用的是Visual Studio 2012(专业版)已实现的ASP.Net Web表单。 I have created an extra page called 'users.asps' with only three text boxes (username, full name and email). 我创建了一个名为“ users.asps”的额外页面,其中只有三个文本框(用户名,全名和电子邮件)。 Then I also created a new database called 'Datatest.mdf' and added ONE table in the database called 'users' which includes three attributes: username (primary key), name and email. 然后,我还创建了一个名为“ Datatest.mdf”的新数据库,并在名为“ users”的数据库中添加了一个表,该表包含三个属性:用户名(主键),名称和电子邮件。

Below is my code for the button on the page. 以下是我在页面上按钮的代码。 Here is where I want to send all the inputted data into my database table. 这是我要将所有输入的数据发送到数据库表的地方。

protected void Button1_Click(object sender, EventArgs e)
{
    string strun = Request.Form["username"];
    string strna = Request.Form["name"];
    string strem = Request.Form["email"];

    OleDbConnection objconnection = null;
    OleDbCommand objcmd = null;
    string stringconnection = null;
    string strSQL = null;

    //connection string

    stringconnection = "provider=SQLOLEDB;data source=(LocalDb)\\v11.0;Initial Catalog=Datatest; Integrated Security=SSPI;";

    objconnection = new OleDbConnection(stringconnection);
    objconnection.ConnectionString = stringconnection;

    objconnection.Open();

    strSQL = "insert into users(username, name, email)values(?,?,?)";
    objcmd = new OleDbCommand(strSQL, objconnection);
    objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@username", strun));
    objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@name", strna));
    objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@email", strem));
    objcmd.ExecuteNonQuery();

    //close connection

    objconnection.Close();
    Response.Write("Entered Successfully");

}

When I run the form however I am getting an error "OleDbException was unhandled by user code" then says "SQL Server does not exist or access denied" that has an arrow pointing at objconnection.Open(); 但是,当我运行表单时,出现错误“用户代码未处理OleDbException”,然后显示“ SQL Server不存在或访问被拒绝”,其中的箭头指向objconnection.Open(); .

Despite where the error is located, I know the problem lies where my string connection is, I just don't know how I can fix it. 尽管错误位于何处,但我知道问题出在我的字符串连接所在的地方,我只是不知道该如何解决。

stringconnection = "provider=SQLOLEDB;data source=(LocalDb)\\v11.0;Initial Catalog=Datatest; Integrated Security=SSPI;";

For all your connectionstring propblems. 对于您的所有连接字符串问题。 You want to take a look in the sql department of that website. 您想查看该网站的sql部门

I think that SQLOLEDB provider doesn't understand the connection string required to use a LocalDB. 我认为SQLOLEDB提供程序不理解使用LocalDB所需的连接字符串。
(The LocalDB is more recent than SQLOLEDB) (LocalDB比SQLOLEDB更新)

Try to use SQLNCLI11 (The Sql Server Native Client v11) 尝试使用SQLNCLI11(SQL Server本机客户端v11)

"provider=SQLNCLI11;data source=(LocalDb)\\v11.0;
 Initial Catalog=Datatest; Integrated Security=SSPI;";

However, if you are in the initial stage of developping your application I really suggest to use the SqlClient classes like SqlConnection, SqlCommand etc and leave behind the OleDb and all its limitations. 但是,如果您正处于开发应用程序的初始阶段,我真的建议您使用SqlClient类(例如SqlConnection,SqlCommand等),并抛弃OleDb及其所有限制。 (Like no support for named parameters) (不支持命名参数)

 using System.Data.SqlClient;
 ......


stringconnection = @"Data Source=(LocalDb)\\v11.0;Initial Catalog=Datatest;
                    Integrated Security=SSPI;";

using(objconnection = new SqlConnection(stringconnection))
{
    objconnection.Open();
    strSQL = "insert into users(username, name, email)values(@username, @name, @email)";
    using(objcmd = new SqlCommand(strSQL, objconnection))
    {
        objcmd.Parameters.Add(new SqlParameter("@username", strun));
        objcmd.Parameters.Add(new SqlParameter("@name", strna));
        objcmd.Parameters.Add(new SqlParameter("@email", strem));
        objcmd.ExecuteNonQuery();
    }
}
Response.Write("Entered Successfully");

Try to visit this link https://www.daniweb.com/software-development/csharp/threads/339949/how-to-establish-a-connection-with-sql-server-using-c 尝试访问此链接https://www.daniweb.com/software-development/csharp/threads/339949/how-to-建立-a-connection-with-sql-server-using-c

maybe it can help you on your database connection problem :) 也许它可以帮助您解决数据库连接问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用C#visual Studio 2008将数据插入表中 - Cannot insert the data into the table using C# visual studio 2008 无法将数据插入表C#visual studio 2013 - Cannot insert the data into the table C# visual studio 2013 插入和注册数据未显示在数据库表 Visual Studio 2019 中 - Insert and Registration data not show in database table visual studio 2019 如何使用Visual Studio和C#将数据插入数据库并将其保存到数据库中? - How can I insert and save data into database using Visual Studio and C#? 使用Visual Studio 2017在SQL数据库中插入数据 - Insert data in SQL Database using Visual Studio 2017 使用C#中的Visual Studio通过MySQL将数据插入数据库 - Insert Data into database via mySQL using visual studio in C# 我已经使用Visual Studio为C#程序创建了一个数据库表。 怎么办? - I've created a database table using Visual Studio for my C# program. Now what? 将数据插入表-Visual Studio中的Web服务 - Insert data to a table - web service in visual studio 插入登录和注册数据未显示在 SQL 服务器数据库表 Visual Studio 2019 - Insert login and registration data not show in SQL Server database table Visual Studio 2019 如何使用C#将数据插入SQL Sever而不使用数据库向导(visual studio) - How To insert data into SQL Sever with C# without using the database wizard(visual studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM