技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營

贊助商

分類目錄

贊助商

最新文章

搜索

asp.net字母大小寫不對引發(fā)“找不到類型或命名空間名稱”錯誤

作者:admin    時(shí)間:2023-4-27 21:10:24    瀏覽:

在 asp.net 中出現(xiàn)“找不到類型或命名空間名稱‘SqlConnection’(是否缺少 using 指令或程序集引用?)”錯誤,經(jīng)檢查,問題起因令人哭笑不得。

問題

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);

        }
    }
}

分析

錯誤在 SqlConnection connection = new SqlConnection(connectionString); 那行,我正在使用 MSVS 2013 和 Microsoft sql server,我嘗試添加 System.Data.SqlClient;

當(dāng)我添加 System.Data.SqlClient 命名空間時(shí),它給出了另一個(gè)錯誤:

錯誤 1 ??'System.Data.SqlClient.SqlConnection' 不包含 'close' 的定義并且沒有擴(kuò)展方法 'close' 接受類型為 'System' 的第一個(gè)參數(shù)(您是否缺少 using 指令或程序集引用?)

解決

問題解決了!

編寫代碼 connection.open();connection.close();的問題,用 connection.Close(); 代替connection.close();,用connection.Open(); 代替connection.open(); ,之前用小寫字母,是不對的!

C#代碼區(qū)分大小寫字母,一個(gè)不經(jīng)意的失誤,導(dǎo)致了問題的產(chǎn)生。

相關(guān)文章

標(biāo)簽: asp.net  CSharp  命名空間  
x