【发布时间】:2016-11-08 01:32:11
【问题描述】:
我是 c# 和 .net 的新手,所以我将数据插入数据库。
首先我创建了新项目 - asp.net Web 应用程序 (v4.6.1)。然后我选择了网络表单模板。
我的web.config 文件自动添加了这一行
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication7-20161108020707.mdf;Initial Catalog=aspnet-WebApplication7-20161108020707;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
这就是我与数据库的连接。
我启动了 Microsoft SQL Server Management Studio。在左侧,我有对象资源管理器,可以在其中看到我的服务器(我的计算机 - 我使用 Windows 身份验证)。有一个名为数据库的文件夹。我右键单击它并选择新数据库...我创建了名为 test 的新数据库。然后我展开测试数据库并右键单击表文件夹并选择新建->表。我添加了 2 列。 id (int) - 非 null 和 name (nchar(255)) - 非 null。我添加了 id 列作为身份。我保存了表并将其命名为 testing。
现在我在 Visual Studio 中创建了Default.aspx 页面。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:TextBox ID="someBox" runat="server" placeholder="enter something"> </asp:TextBox>
<asp:Button ID="clickMe" runat="server" text="click" onclick=clickMe_click />
</asp:Content>
我添加到 Default.aspx.cs 中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
using System.Data;
namespace WebApplication7
{
public partial class _Default : Page
{
protected void clickMe_click(object sender, EventArgs e)
{
string txtBox = someBox.Text;
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("DefaultConnection");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT testing (name) VALUES (txtBox)";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
}
}
我也尝试在点击函数中使用这段代码:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO testing (name) VALUES (@name)";
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = txtBox;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
页面刷新以及当我将表刷新到 SQL Server Management Studio 并进行查询时:
select * from testing;
GO
它返回 0 行受影响并显示空表。
我做错了什么?
编辑:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication3-20161105080932.mdf;Initial Catalog=aspnet-WebApplication3-20161105080932;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
aspnet-WebApplication3-20161105080932 是我在服务器对象资源管理器中的数据库名称。在 db 中,我创建了表测试。
这是我的cs代码:
protected void clickMe_click(object sender, EventArgs e)
{
string txtBox = someBox.Text;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
{
string sql = "INSERT INTO testing (name) VALUES (@name)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("name", SqlDbType.NVarChar).Value = txtBox;
int rowsAffected = cmd.ExecuteNonQuery();
conn.Close();
}
}
但是什么也没发生。
【问题讨论】:
-
@un-lucky,他已经在这样做了。查看发布的代码。
-
字符串 txtBox = someBox.Text;我把它放到字符串 txtBox 中,所以我在代码的其他部分使用 txtBox。
标签: c# asp.net .net sql-server database