[英]asp.net c# data to a database
I have been at this for two days now with no luck. 我已经在这两天了,没有运气。 The problem is I am trying to input data into my sql server database via a web form.
问题是我试图通过Web表单将数据输入我的sql server数据库。 every time I try to run I am getting errors .
每次我试图跑,我都会收到错误。
Bellow is the Error I am Getting when I run the code Bellow是我运行代码时遇到的错误
this is the code for the web form 这是Web表单的代码
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.Data;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cs = new SqlConnection ("Data Source = SQLEXSPRESS; Initial Catalog = OMS; Integrated Security = true");
SqlDataAdapter da = new SqlDataAdapter ();
da.InsertCommand = new SqlCommand("INSERT INTO Customer tbl (FirstName,LastName) Customer VALUES (@FirstName,@LastName)", cs);
da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = firstname.Text;
da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = lastname.Text;
cs.Open();
da.InsertCommand.ExecuteNonQuery(); // Error occurs here
cs.Close();
}
protected void firstname_TextChanged(object sender, EventArgs e)
{
}
protected void lastname_TextChanged(object sender, EventArgs e)
{
}
}
}
This is my database Table code 这是我的数据库表代码
CREATE TABLE [dbo].[Customer] (
[CustomerID] INT IDENTITY (1, 1) NOT NULL,
[FirstName] VARCHAR (50) NULL,
[LastName] VARCHAR (50) NULL,
[Address] VARCHAR (50) NOT NULL,
[City] VARCHAR (25) NOT NULL,
[Postcode] VARCHAR (10) NOT NULL,
[Country] VARCHAR (50) NOT NULL,
[Modified] ROWVERSION NOT NULL,
PRIMARY KEY CLUSTERED ([CustomerID] ASC)
); );
Any help will be greatly appreciated. 任何帮助将不胜感激。
SqlConnection cs = new SqlConnection ("Data Source = SQLEXSPRESS; Initial Catalog = OMS; Integrated Security = true");
Should be 应该
SqlConnection cs = new SqlConnection ("Data Source = SQLEXPRESS; Initial Catalog = OMS; Integrated Security = true");
You spelt express wrong! 你拼写错了!
You might also need to use Data Source = .\\SQLEXPRESS 您可能还需要使用Data Source =。\\ SQLEXPRESS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.