System.InvalidOperationException连接到SQLExpress数据库?

时间:2017-04-16 17:07:09

标签: asp.net

<!DOCTYPE html>
<html>


<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="script.js"></script>
  <link rel="stylesheet" href="style.css">
  <meta name="viewport" content="width=10, initial-scale=1.0">
</head>

<body>
  <label>
    <div class="side">
      Amount:
      <input type="number" class="numberOfStocks" value="1" min="0" />
    </div>
    <br>
    <br>
    <br>
    <br>
  </label>

  <div>
    <div class="sideone">
      <h2>First Item<span class="servingUnit"></span>:</h2>
      <select class="selectStock">
        <option value="-1">Pick a food!</option>
      </select>
    </div>
    <label>
      <div class="side">
        Amount:
        <input type="number" class="numberOfStocks" value="1" min="0" />
      </div>
      <br>
      <br>
      <br>
      <br>
    </label>
    <div class="sideone">
      <h2>Second Item<span class="servingUnit"></span>:</h2>
      <select class="selectStock">
        <option value="-1">Pick a food!</option>
      </select>
      <br>
      <br>
    </div>
    <div class="comparison side">
      <h2>Comparison:</h2>
      <br>
      <br>
      <div class="result"></div>
    </div>
  </div>
</body>

</html>

enter image description here

错误在protected void btnSubmit_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source= LAPTOP-KVFS4TPD\\SQLEXPRESS; Database= PayPalDB; UID= sa; PWD= 061199081298;"); SqlCommand cmd = new SqlCommand("SP_DBASE", con); cmd.CommandType = CommandType.StoredProcedure; con.Open(); cmd.Parameters.AddWithValue("@FirstName", FNtxt.Text); cmd.Parameters.AddWithValue("@MiddleName", MNtxt.Text); cmd.Parameters.AddWithValue("@LastName", LNtxt.Text); cmd.Parameters.AddWithValue("@Email", Emailtxt.Text); cmd.Parameters.AddWithValue("@Email2", Email2txt.Text); cmd.Parameters.AddWithValue("@BirthDate", txtDate.Text); cmd.Parameters.AddWithValue("@Address", Addresstxt.Text); cmd.Parameters.AddWithValue("@CreditCardNo", CCNtxt.Text); cmd.Parameters.AddWithValue("@CVVNo", CVVtxt.Text); cmd.Parameters.AddWithValue("@Gender", GenderRDL.SelectedItem); cmd.Parameters.AddWithValue("@Country", DDLCountry.SelectedItem); cmd.ExecuteNonQuery(); con.Close(); Server.Transfer("UserLogIn.aspx", true); }

  

System.Data.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理“。

1 个答案:

答案 0 :(得分:0)

您的连接字符串错误。

这是SQLExpress连接字符串的正确语法:

Data Source=.\SQLEXPRESS;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

在你的情况下:

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=PayPalDB;UID=sa;PWD=061199081298;");