|
|
|
|
|
在 asp.net 中出現(xiàn)“找不到類(lèi)型或命名空間名稱(chēng)‘SqlConnection’(是否缺少 using 指令或程序集引用?)”錯(cuò)誤,經(jīng)檢查,問(wèn)題起因令人哭笑不得。
C#程序代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace studentA
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void saveButton_Click(object sender, EventArgs e)
{
string name = nameTextBox.Text;
string email = emailTextBox.Text;
string phone = phoneTextBox.Text;
string reg = regTextBox.Text;
Student student = new Student(name, email, phone, reg);
string connectionString = @"Server=.\SQLEXPRESS;Database=db;Integrated Security=true";
SqlConnection connection = new SqlConnection(connectionString);
string query = "INSERT INTO Students VALUES('" + student.Name + "','" + student.Email + "','" + student.Phone + "','" + student.Reg + "')";
SqlCommand command = new SqlCommand(query, connection);
connection.open();
int rowAffected=command.ExecuteNonQuery();
connection.close();
if (rowAffected > 0)
{
messageLabel.Text = "success";
}
else
{
messageLabel.Text = "failed";
}
Response.Write(rowAffected);
}
}
}
錯(cuò)誤在 SqlConnection connection = new SqlConnection(connectionString); 那行,我正在使用 MSVS 2013 和 Microsoft sql server,我嘗試添加 System.Data.SqlClient;。
當(dāng)我添加 System.Data.SqlClient 命名空間時(shí),它給出了另一個(gè)錯(cuò)誤:
錯(cuò)誤 1 ??'System.Data.SqlClient.SqlConnection' 不包含 'close' 的定義并且沒(méi)有擴(kuò)展方法 'close' 接受類(lèi)型為 'System' 的第一個(gè)參數(shù)(您是否缺少 using 指令或程序集引用?)
問(wèn)題解決了!
編寫(xiě)代碼 connection.open(); 和connection.close();的問(wèn)題,用 connection.Close(); 代替connection.close();,用connection.Open(); 代替connection.open(); ,之前用小寫(xiě)字母,是不對(duì)的!
C#代碼區(qū)分大小寫(xiě)字母,一個(gè)不經(jīng)意的失誤,導(dǎo)致了問(wèn)題的產(chǎn)生。
相關(guān)文章