无法找到SQLConnection

时间:2015-08-19 09:42:58

标签: asp.net sql-server

我是一个在我的生命中第一次在MSSQL Server和ASP.Net中工作的网页设计。我正在尝试使用MVC将数据库连接到表单。当我明白必须将连接从默认的Visual Studio更改为代码时,我的工作才真正完成。 (老板告诉我!)

我只是搜索以了解如何做到这一点,我发现了ADO.NET。所以我试图改变我的代码,但它不能识别SQLConnection(红色波浪线表示它无法找到),但我真的不知道为什么。我已经搜索但没有任何帮助我。你能帮帮我吗?

非常感谢!

上一个代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    using MVCEmail.Models;
    using System.Net;
    using System.Net.Mail;
    using System.Threading.Tasks;
    using DQA_DBTemplate.Model;
    using DQA_DBTemplate.DAL.Repositories;
    using DQA_DBTemplate.DAL.Data;

    (...)
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ViewResult Contacts(EmailFormModel _objModelMail){
       if (ModelState.IsValid){
           Email e = new Email();
           e.Address = _objModelMail.FromEmail;
           e.Message = _objModelMail.Message;
           e.Subject = _objModelMail.FromSubject;
           e.Date = DateTime.Now.ToString();

           try {
              MailMessage mail = new MailMessage();
              mail.To.Add(_objModelMail.FromEmail);
              mail.From = new MailAddress("teste_mvc@dqadesign.com");
              mail.Subject = _objModelMail.FromSubject;
              string Body = _objModelMail.Message;
              mail.Body = Body;

              SmtpClient smtp = new SmtpClient();
              smtp.Host = "mail.dqadesign.com";
              smtp.Port = 587;
              smtp.UseDefaultCredentials = false;
              smtp.Credentials = new System.Net.NetworkCredential
              ("teste_mvc@dqadesign.com", "myPassword");
              smtp.Send(mail);
              e.Sent = true;
              email.Insert(e);
              email.Commit();
              return View("Index");
          }
          catch (Exception ex){
              e.Sent = false;
              email.Insert(e);
              email.Commit();
               return View("News");
          }
      }
      return View();
   }

  (...)

实际代码(当然未完成):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MVCEmail.Models;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using DQA_DBTemplate.Model;
using DQA_DBTemplate.DAL.Repositories;
using DQA_DBTemplate.DAL.Data;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
(...)
[HttpPost]
[ValidateAntiForgeryToken]
public ViewResult Contacts(EmailFormModel _objModelMail){
    if(ModelState.IsValid){
       using(var sqlConnection = new SqlConnection("DefaultConnection")){
          var query = "INSERT INTO Emails (@Address,@Subject,@Message,@Date,@Sent)";
          using(var sqlCommand = new SqlCommand(query,sqlConnection)){
                 sqlConnection.Open();

                 sqlCommand.Parameters.AddWithValue("@Address", _objModelMail.FromEmail);
                 sqlCommand.Parameters.AddWithValue("@Subject", _objModelMail.FromSubject);
                 sqlCommand.Parameters.AddWithValue("@Message", _objModelMail.Message);
                 sqlCommand.Parameters.AddWithValue("@Date", DateTime.Now.ToString());
                 sqlCommand.ExecuteNonQuery();
                 sqlConnection.Close();
          }
      }
  }
  }
  (...)

2 个答案:

答案 0 :(得分:1)

SqlConnection课程住在System.Data.SqlClient内。添加using语句:

using System.Data.SqlClient;

或者使用完整的命名空间:

var sqlConnection = new System.Data.SqlClient.SqlConnection("DefaultConnection");

另外,对于代码的评论,最好不要将Parameters.AddWithValue用于various reasons

答案 1 :(得分:-1)

using(var conn=new. SqlConnection( "server=(local);Initial Catalog=dbName;Integrated Security=True"))
{
  conn.Open();
}

string connectionString = "server=(local);Initial Catalog=dbName;Integrated Security=True"
using(var conn=new. SqlConnection(connectionString )
{
  conn.Open();
}

使用您的连接字符串更改上述连接字符串,您必须导入

  

使用System.Configuration;