[英]asp.net c# data to a database
我已經在這兩天了,沒有運氣。 問題是我試圖通過Web表單將數據輸入我的sql server數據庫。 每次我試圖跑,我都會收到錯誤。
Bellow是我運行代碼時遇到的錯誤
這是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)
{
}
}
}
這是我的數據庫表代碼
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)
);
任何幫助將不勝感激。
SqlConnection cs = new SqlConnection ("Data Source = SQLEXSPRESS; Initial Catalog = OMS; Integrated Security = true");
應該
SqlConnection cs = new SqlConnection ("Data Source = SQLEXPRESS; Initial Catalog = OMS; Integrated Security = true");
你拼寫錯了!
您可能還需要使用Data Source =。\\ SQLEXPRESS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.