我的情况是这样的。我有一个需要更新的电话表(如果他们需要更改客户端的现有电话表)或删除(如果他们希望删除现有的号码)或插入(如果字段为空,则插入新的否)。 我已经有两个存储过程,它们添加一个新电话并更新现有电话。其参数为:
EXECUTE @RC = [CUSTOMER_test].[dbo].[uspAddClientTel]
@ClientID
,@TelNo
,@TelTypeID
,@DetailsTypeID
,@SortNo
,@ResultTelID OUTPUT
EXECUTE @RC = [CUSTOMER_test].[dbo].[uspUpdateClientTel]
@TelID
,@TelNo
,@TelTypeID
,@DetailsTypeID
,@SortNo
,@ResultTelID OUTPUT
GO
表单视图中的部分查询
(SELECT TOP 1 F.tel_no FROM TELEPHONE as F where F.tel_type_id = 3 AND F.client_id = @id ORDER BY sort_no ) AS fax,
(SELECT TOP 1 T.tel_no from TELEPHONE as T where T.tel_type_id = 1 OR T.tel_type_id = 2 AND T.client_id = @id ORDER BY sort_no) AS telephone,
(SELECT TOP 1 F.tel_id FROM TELEPHONE as F where F.tel_type_id = 3 AND F.client_id = @id ORDER BY sort_no ) AS faxid ,
(SELECT TOP 1 T.tel_id from TELEPHONE as T where T.tel_type_id = 1 OR T.tel_type_id = 2 AND T.client_id = @id ORDER BY sort_no) AS telephoneid ,
CLIENT_ADDRESS.client_address_id
FROM ORGANIZATION AS O INNER JOIN CLIENT ON O.client_id = CLIENT.client_id
LEFT OUTER JOIN CLIENT_ADDRESS ON CLIENT.client_id = CLIENT_ADDRESS.client_id
WHERE (CLIENT.client_id = @id)"
所以我有电话(如固定电话/移动电话)和传真及其ID,如果它们已存在于数据库中。
现在我需要创建一个接受参数的函数,从而根据传递给它的参数决定使用哪个sp。 我完全迷失在这里。任何有关如何使用此功能的帮助将不胜感激。甚至链接到教程将有所帮助! 再次感谢!
P.S 在尝试这个功能之前,我做了类似的事情,但它并不一致
protected void FrmClient_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
string TelTypeID;
string ResultTelID1 = "";
int intClientID = (int)e.Keys[0];
string strTelID = (string)e.OldValues["telephoneid"];
string strFaxID = (string)e.OldValues["faxid"];
SqlConnection conTel = new SqlConnection();
conTel.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["CUSTOMERConnectionString"]);
conTel.Open();
if (strTelID != "")
{
string strtelcmd = ("SELECT tel_type_id, details_type_id, sort_no from TELEPHONE where tel_id =" + strTelID);
SqlCommand telcmd = new SqlCommand(strtelcmd, conTel);
SqlDataReader rdrTel = telcmd.ExecuteReader();
while (rdrTel.Read())
{
sdsClient.UpdateParameters["TelTypeID"].DefaultValue = rdrTel["tel_type_id"].ToString();
sdsClient.UpdateParameters["DetailsTypeID"].DefaultValue = rdrTel["details_type_id"].ToString();
sdsClient.UpdateParameters["SortNo"].DefaultValue = rdrTel["sort_no"].ToString();
}
rdrTel.Close();
}
else if (strFaxID != "")
{
string strfaxcmd = ("SELECT tel_type_id, details_type_id, sort_no from TELEPHONE where tel_id ="+ strFaxID);
SqlCommand faxcmd = new SqlCommand(strfaxcmd, conTel);
SqlDataReader rdrfax = faxcmd.ExecuteReader();
while (rdrfax.Read())
{
sdsClient.UpdateParameters["TelTypeID1"].DefaultValue = rdrfax["tel_type_id"].ToString();
sdsClient.UpdateParameters["DetailsTypeID1"].DefaultValue = rdrfax["details_type_id"].ToString();
sdsClient.UpdateParameters["SortNo1"].DefaultValue = rdrfax["sort_no"].ToString();
}
rdrfax.Close();
}
else if (strTelID == "")
{
SqlCommand cmdaddTel = new SqlCommand("uspAddClientTel",conTel);
cmdaddTel.CommandType = CommandType.StoredProcedure;
string strNewTel = (string)e.NewValues["telephone"];
if (strNewTel.Trim().StartsWith("06") || strNewTel.Trim().StartsWith("07") )
TelTypeID = "2";
else
TelTypeID = "1";
cmdaddTel.Parameters.Add(new SqlParameter("@ClientID", intClientID));
cmdaddTel.Parameters.Add(new SqlParameter("@TelNo",strNewTel ));
cmdaddTel.Parameters.Add(new SqlParameter("@TelTypeID", TelTypeID));
cmdaddTel.Parameters.Add(new SqlParameter("@DetailsTypeID", 1));
cmdaddTel.Parameters.Add(new SqlParameter("@SortNo1", 1 ));
cmdaddTel.Parameters.Add(new SqlParameter("@ResultTelID1", ResultTelID1));
SqlDataReader rdrAddFax = cmdaddTel.ExecuteReader();
}
else if (strFaxID == "")
{
SqlCommand cmdaddFax = new SqlCommand("uspAddClientTel", conTel);
cmdaddFax.CommandType = CommandType.StoredProcedure;
string strNewFax = (string)e.NewValues["fax"];
cmdaddFax.Parameters.Add(new SqlParameter("@ClientID", intClientID));
cmdaddFax.Parameters.Add(new SqlParameter("@TelNo", strNewFax));
cmdaddFax.Parameters.Add(new SqlParameter("@TelTypeID", 3));
cmdaddFax.Parameters.Add(new SqlParameter("@DetailsTypeID", 2));
cmdaddFax.Parameters.Add(new SqlParameter("@SortNo1", 1));
cmdaddFax.Parameters.Add(new SqlParameter("@ResultTelID1", ResultTelID1));
SqlDataReader rdrAddFax = cmdaddFax.ExecuteReader();
}
conTel.Close();
}
和formview的updatecommand中的sps。
答案 0 :(得分:0)
你能不把这个逻辑纳入SP吗?那么根据传递给SP的参数,您可以插入或更新必要的记录吗?
我知道您建议您希望在业务逻辑中完成此操作,但由于您似乎没有使用ORM作为数据访问层,因此您可以快速保持ID(因为您只需要这样做)单程往返db)并在SP中整理?
答案 1 :(得分:0)
嗨,我最后还是把它解决了。认为这可能对像我这样的初学者有帮助
public void functUAD(int intClientID, string TelType, int TelID, string OldTelNumber, string NewTelNumber)
{
int ResultTelID = 0 ;
int TelTypeID ;
int Result= 0 ;
SqlConnection conTel = new SqlConnection();
conTel.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["CUSTOMERConnectionString"]);
conTel.Open();
if (TelType.ToLower() == "fax")
TelTypeID = 3;
else if (NewTelNumber.Trim().StartsWith("06") || NewTelNumber.Trim().StartsWith("07"))
TelTypeID = 2;
else
TelTypeID = 1;
if (OldTelNumber != "" && NewTelNumber != "" ) //update old number with new number
{
SqlCommand cmdUpdateTel = new SqlCommand("uspUpdateClientTel", conTel);
cmdUpdateTel.CommandType = CommandType.StoredProcedure;
SqlCommand cmdSortNo = new SqlCommand("SELECT sort_no FROM TELEPHONE WHERE tel_id= " + TelID, conTel);
string SortNo = (string)cmdSortNo.ExecuteScalar();
cmdUpdateTel.Parameters.Add(new SqlParameter("@TelID", TelID));
cmdUpdateTel.Parameters.Add(new SqlParameter("@TelNo", NewTelNumber));
cmdUpdateTel.Parameters.Add(new SqlParameter("@TelTypeID", TelTypeID));
cmdUpdateTel.Parameters.Add(new SqlParameter("@DetailsTypeID", 1));
cmdUpdateTel.Parameters.Add(new SqlParameter("@SortNo", SortNo));
cmdUpdateTel.Parameters.Add(new SqlParameter("@ResultTelID", ResultTelID));
cmdUpdateTel.ExecuteNonQuery();
}
if (OldTelNumber == "" && NewTelNumber != "") // add a newnumber
{
int SortNo = 0 ;
SqlCommand cmdaddTel = new SqlCommand("uspAddClientTel", conTel);
cmdaddTel.CommandType = CommandType.StoredProcedure;
if (TelTypeID == 1)
SortNo = 1;
else
{
SqlCommand cmdSortNo = new SqlCommand("SELECT MAX(sort_no) from TELEPHONE where tel_id =" + TelID, conTel);
int MaxSort = (int)cmdSortNo.ExecuteScalar();
SortNo += MaxSort;
}
cmdaddTel.Parameters.Add(new SqlParameter("@ClientID", intClientID));
cmdaddTel.Parameters.Add(new SqlParameter("@TelNo", NewTelNumber));
cmdaddTel.Parameters.Add(new SqlParameter("@TelTypeID", TelTypeID));
cmdaddTel.Parameters.Add(new SqlParameter("@DetailsTypeID", 1));
cmdaddTel.Parameters.Add(new SqlParameter("@SortNo", SortNo ));
cmdaddTel.Parameters.Add(new SqlParameter("@ResultTelID", ResultTelID));
cmdaddTel.ExecuteNonQuery();
}
if (OldTelNumber != "" && NewTelNumber == "") // delete the old number
{
SqlCommand cmdDelete = new SqlCommand("RemoveClientTel", conTel);
cmdDelete.Parameters.Add(new SqlParameter("@TelID",TelID));
cmdDelete.Parameters.Add(new SqlParameter("@Result", Result));
cmdDelete.ExecuteNonQuery();
}
}
然后你可以使用适当的参数调用该函数!再见!