SQL语句检查连接值和返回语句(如果未找到)

时间:2017-11-27 18:31:57

标签: sql azure-sql-database

我正在尝试使用CustomerID和ServID搜索两列表。主键是两者的组合。我希望查询搜索组合是否存在。如果它是返回的东西,我可以在c#中取消输入,或者如果没有找到它,则插入记录。这是我经过几个小时的搜索后做到的。

IF (SELECT * FROM Cus_Ser WHERE customerID=inputnumber AND ServID=inputnumber) IS NULL
   PRINT 'Already Exists'
ELSE
   INSERT INTO Cus_Ser(CustomerID,ServID) VALUES(inputnumber, inputnumber)

这是代码

@using WebMatrix.Data
@{
var CustomerID = Request.QueryString["CustomerID"];
var ServID = 0;
var db = Database.Open("Azure");
var selectedData = db.Query("SELECT * FROM Customer INNER JOIN Cus_Ser ON Customer.customerID=Cus_Ser.customerID INNER JOIN Service ON Cus_Ser.ServID=Service.ServID WHERE customer.CustomerID =" + CustomerID);
var grid = new WebGrid(source: selectedData);
var selectedData2 = db.Query("SELECT * FROM Service");
var grid2 = new WebGrid(source: selectedData2);

if (IsPost)
{ if (int.TryParse(Request.Form["ServID"], out int numbertest))
    {
        ServID = Int32.Parse(Request.Form["ServID"]);
        var dbCommandSearch = "SELECT * FROM Cus_Ser WHERE customerID=" + CustomerID + " AND ServID=" + ServID;
        //search = db.QuerySingle(dbCommandSearch);
        //if ((CustomerID, "ServID") != search    
        db = Database.Open("Azure");
        var insertCommand = "INSERT INTO cus_ser (customerID,ServID)VALUES(" + CustomerID + ", " + ServID + ")";
        db.Execute(insertCommand);
        Response.Redirect("~/ViewService?CustomerID=" + CustomerID);
    }
    //else
    //{
    //Validation.AddFormError("Entered Value is Not a Service Numberr");
    //}

    else
    {
        Validation.AddFormError("Entered Value is NOT a Service Number");
    }
} }

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
    <title>Add Service</title>
<style>
    .validation-summary-errors {
        border: 2px dashed red;
        color: red;
        font-weight: bold;
        margin: 12px;
    }
</style>
</head>
<body>
<form method="post">
    <fieldset>
<legend>Input Service To Add</legend>
        <p>
            <label for="ServID">Service ID:</label>
            <input type="text" name="ServID" value="@ServID" />
        </p>

        <input type="hidden" name="CustomerID" value="@CustomerID" />

        <p><input type="submit" name="buttonSubmit" value="Submit Changes" /></p>
    </fieldset>
</form>
<p><a href="~/ViewCustomers">Return to customer listing</a></p>

   

0 个答案:

没有答案