嘿傢伙我在視覺工作室2012內使用連接字符串數據庫設置。 我想發送數據到數據庫沒有錯誤的代碼,但它沒有運行,任何幫助會很棒,因爲我現在已經呆了幾個小時,需要睡覺。 謝謝。數據輸入asp.net C#SQL
默認頁面代碼
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 = (LocalDB)\v11.0; Initial Catalog = Database1; Integrated Security = true");
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("INSERT INTO Adopter (FIRSTNAME,LASTNAME) 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();
cs.Close();
}
protected void firstname_TextChanged(object sender, EventArgs e)
{
}
protected void lastname_TextChanged(object sender, EventArgs e)
{
}
}
}
SQL代碼
CREATE TABLE [dbo].[Adopter] (
[FIRSTNAME] VARCHAR (50) NOT NULL,
[LASTNAME] VARCHAR (50) NOT NULL,
);
我知道這將是一些簡單的...我只是不能看到它。
它是否寫入您的表格?你甚至能夠連接到數據庫? 「它沒有運行」,對我們來說不足以幫助你。 – Brian
Web應用程序加載允許我鍵入文本框,當我點擊按鈕它說cs.open有問題。所以它不會將數據輸入到數據庫表中。 – Beep
您需要在連接字符串中指定您的'DataSource'(數據庫實例)以及'Initial Catalog'(這是數據庫名稱(您已經有了))。 – Brian