0

messageID だけを返したいのですが、正しく動作しません。単一の文字列値を返すにはどうすればよいですか?

    [WebMethod]
    public string messageComeGetID(string from, string to)
    {
        try
        {
            sqlConn.Open();
        }
        catch (Exception e)
        {
            return null;
        }
        // select messageID from tblMessage where [from] = '2' AND [to] = '4' gibi.
        SqlCommand cmd3 = new SqlCommand("select messageID from tblMessage where [from]=@from AND [to]=@to", sqlConn);

        cmd3.Parameters.AddWithValue("@from", from);
        cmd3.Parameters.AddWithValue("@to", to);

        cmd3.Connection = sqlConn;
        object value = cmd3.ExecuteScalar();
        string messageID = Convert.ToString(value);
        cmd3.ExecuteNonQuery();
        cmd3.Dispose();
        sqlConn.Close(); 

        return messageID;
    }
4

1 に答える 1