如何在C#中编写SQL代码以发送错误消息

时间:2012-05-16 17:21:39

标签: c# sql

我有代码根据用户上传excel电子表格,然后一旦上传,我就会根据另一个SQL表更新SQL中的一个colummns。我想验证Excel电子表格中的数据是否与SQL表中的某个更新选项相匹配。什么是最简单的方法?即如果返回NULL值,我是否可以在网站上添加一条SQL语句给我一条错误消息,或者我是否必须在C#中编写复杂的IF语句?这是我到目前为止(参见SQL语句):

try
    {

        FileUpload.SaveAs(path);
        if (TextBox.Text != "")
        sheetname = TextBox.Text;
        System.Data.OleDb.OleDbConnection MyConnection;
        System.Data.DataSet DtSet;
        System.Data.OleDb.OleDbDataAdapter MyCommand;  //check sheet name
        MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetname + "$]", MyConnection);
        //MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [July Noms$]", MyConnection);
        MyCommand.TableMappings.Add("Table", "TestTable");
        DtSet = new System.Data.DataSet();
        MyCommand.Fill(DtSet);
        SqlConnection curConnection = new SqlConnection(@"Data Source=1tc-Techcomm2;Initial Catalog=EventRegistration;Integrated Security=True");
        curConnection.Open();
        SqlCommand curCommand;
        SqlParameter param;
        string str;

        for (int i = 0; i < DtSet.Tables[0].Rows.Count; i++)
        {
            if (DtSet.Tables[0].Rows[i][1] == DBNull.Value)
                continue;
            curCommand = curConnection.CreateCommand();
            curCommand.CommandText = @"Insert into TestSP (SessionHead_EventId, SessionHead_Title, SessionHead_TypeId, SessionHead_Description, SessionHead_Status, SessionHead_Presenter, SessionHead_Champion, SessionHead_AnnounceDate, SessionHead_Objectives, SessionHead_LaunchDate, SessionHead_Notes, SessionHead_Schedule, SessionHead_SpecialNeeds, SessionHead_Equipment, SessionHead_CDA) Values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n,@o)";
            for (int j = 0; j < 15; j++)
            {
                param = new SqlParameter();
                str = "@";
                str += (char)('a' + j);
                param.ParameterName = str;
                param.SqlDbType = SqlDbType.VarChar;
                param.Value = DtSet.Tables[0].Rows[i][j];
                curCommand.Parameters.Add(param);

            }
            Label1.Text = "THE EXCEL DATE HAS SUCCESSFULLY BEEN SENT TO THE DATABASE";
            curCommand.ExecuteNonQuery();
            curCommand.CommandText = "UPDATE TestSP SET SessionHead_TypeId = ( SELECT SessionType.SessionType_Id FROM SessionType WHERE SessionType.SessionType_Title = TestSP.SessionHead_TypeId) WHERE EXISTS ( SELECT SessionType.SessionType_Id FROM SessionType WHERE SessionType.SessionType_Title = TestSP.SessionHead_TypeId)"; 
//****Can I add a statement here stating that if a NULL value is returned, to give me an error message on the website?****

        curCommand.ExecuteNonQuery();

        }


        MyConnection.Close();
        curConnection.Close();

    }
    catch (Exception ex)
    {
        //Response.Write(ex.ToString());
        Label1.Text = ex.Message;
        Label2.Text = ex.Message; 

    }


}

任何帮助都将非常感谢!!

2 个答案:

答案 0 :(得分:1)

我建议您使用存储过程返回sql错误消息。

答案 1 :(得分:0)

如果查询为false,

catch System.Data.SqlClient.SqlException会给出错误消息。 此外,ExecuteNonQuery将为受影响的行提供整数。因此,0如果没有受影响的行,-1如果出现错误,例如回滚。