-1
因此,我一直致力於在頁面上允許用戶提交內容,並且頁面上的所有內容似乎都可以工作,但是當涉及到與SQL服務器的通信時,錯誤一直在拋出。
錯誤提示該進程已被終止,因爲字符串或二進制文件將被截斷。
SQL Server:字符串或二進制文件將被截斷ASP.NET
這是我用來處理與服務器通信的代碼。
using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF"";Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("INSERT INTO aspnet_newscontent (author, username, date, category, content, description) VALUES (@author, @username, @date, @category, @content, @description)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = connection;
if (nameInput.Text != "")
{
cmd.Parameters.AddWithValue("@author", nameInput.Text);
}
else if (nameInput.Text == "")
{
cmd.Parameters.AddWithValue("@author", "Anonymous");
}
cmd.Parameters.AddWithValue("@username", userId);
cmd.Parameters.AddWithValue("@date", DateTime.Today);
cmd.Parameters.AddWithValue("@category", categoryId);
cmd.Parameters.AddWithValue("@content", content.Text);
cmd.Parameters.AddWithValue("@description", description.Text);
connection.Open();
cmd.ExecuteNonQuery();
}
您的文本列的長度比您要插入的文本短。你的桌子方案是什麼? – Dai
我認爲你應該設置內容和描述列的長度爲最大 –
嘗試放置一個斷點並檢查你所放置的值高於你在'SQL表中定義的範圍。也可以嘗試從SQL插入數據並檢查它是否正確插入,或者在_which_列中顯示的值超過期望值。 – BNN