2013-08-04 74 views
5
private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection conn = new SqlConnection(); 
      conn.ConnectionString = "Data Source=*******;Initial Catalog=ChatApp;User ID=Chatapplication;Password=****"; 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand(); 
      string chatroomidno = textBox1.Text; 
      string chatroomname = textBox2.Text; 
      //cmd.CommandText = "Select [email protected],[email protected] from tblChatRoom"; 
      //cmd.Connection = conn; 
      SqlDataAdapter adapt = new SqlDataAdapter("Chatroomapp",conn); 
      adapt.SelectCommand.CommandType = CommandType.StoredProcedure; 
      DataSet ds=new DataSet(); 
      DataTable dt = new DataTable(); 
      adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomID", SqlDbType.VarChar, 100)); 
      adapt.SelectCommand.Parameters["@ChatRoomID"].Value = chatroomidno; 
      adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomName", SqlDbType.VarChar, 50)); 
      adapt.SelectCommand.Parameters["@ChatRoomName"].Value = chatroomname; 
      adapt.Fill(ds, "tblChatRoom"); 
      if (dt.Rows.Count > 0) 
      { 
       MessageBox.Show("Connection Succedded"); 
      } 
      else 
      { 
       MessageBox.Show("Connection Fails"); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error", ex.Message); 
     } 
    } 

在編譯程序我只拿到了連接失敗消息框,在數據庫 我找到正確調用存儲過程,如何克服程序來獲取連接succeded消息框如何使用ado.net

回答

7

那麼,你正在填充ds數據集 - 但是,然後你檢查dt數據表是否存在行......當然,這絕不會起作用!

如果您只需要一個DataTable - 只需使用並填寫該數據表單 - 無需DataSet的開銷。此外,把你的SqlConnectionSqlCommand到使用塊這樣的:

using (SqlConnection conn = new SqlConnection("Data Source=*******;Initial Catalog=ChatApp;User ID=Chatapplication;Password=****")) 
using (SqlCommand cmd = new SqlCommand("Chatroomapp", conn)) 
{ 
    string chatroomidno = textBox1.Text; 
    string chatroomname = textBox2.Text; 

    SqlDataAdapter adapt = new SqlDataAdapter(cmd); 
    adapt.SelectCommand.CommandType = CommandType.StoredProcedure; 
    adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomID", SqlDbType.VarChar, 100)); 
    adapt.SelectCommand.Parameters["@ChatRoomID"].Value = chatroomidno; 
    adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomName", SqlDbType.VarChar, 50)); 
    adapt.SelectCommand.Parameters["@ChatRoomName"].Value = chatroomname; 

    // fill the data table - no need to explicitly call `conn.Open()` - 
    // the SqlDataAdapter automatically does this (and closes the connection, too) 
    DataTable dt = new DataTable(); 
    adapt.Fill(dt); 

    if (dt.Rows.Count > 0) 
    { 
     MessageBox.Show("Connection Succedded"); 
    } 
    else 
    { 
     MessageBox.Show("Connection Fails"); 
    } 
} 

而只是因爲你在dt.Rows回到沒有行並不一定意味着你的連接失敗.....它可能只是有沒有與您的搜索Critieria相匹配的行!連接工作得很好 - 但SQL命令沒有返回任何行。

0

連接失敗意味着您的程序和數據庫之間出現問題。沒有記錄返回並不意味着連接失敗。這只是表示你的表是空的 - 它不包含任何記錄。

使用ADO.NETstored procedures會與您所做的有所不同。如果您需要檢查連接是否失敗,最好檢查catch部件中返回的異常類型。

以下是我將如何做到這一點。我創建了會怎樣處理我的電話一個單獨的方法,然後在你的button1_Click我剛纔調用此方法:

public async Task<ChatRoom> GetAsync(string chatRoomId, string chatRoomName) 
{ 
    try 
    { 
      string connectionString = ConfigurationManager.ConnectionStrings["Db"].ConnectionString; 

      using (SqlConnection sqlConnection = new SqlConnection(connectionString)) 
      { 
       await sqlConnection.OpenAsync(); 

       using (SqlCommand sqlCommand = new SqlCommand("ChatRooms_Get", sqlConnection)) 
       { 
        sqlCommand.CommandType = CommandType.StoredProcedure; 
        sqlCommand.Parameters.Add(new SqlParameter("@ChatRoomID", chatRoomId)); 
        sqlCommand.Parameters.Add(new SqlParameter("@ChatRoomName", chatRoomName)); 

        using (SqlDataReader sqlDataReader = await sqlCommand.ExecuteReaderAsync()) 
        { 
         ChatRoom chatRoom = null; 

         if (await sqlDataReader.ReadAsync()) 
         { 
           chatRoom = new ChatRoom(); 
           chatRoom.Id = sqlDataReader.GetFieldValue<string>(0); 
           chatRoom.Name = sqlDataReader.GetFieldValue<string>(1); 

           chatRooms.Add(chatRoom); 
         } 

         return chatRoom; 
        } 
       } 
      } 
    } 
    catch (Exception exception) 
    { 
      // Try checking if the connection failed here 

      throw exception; 
    } 
} 

我的聊天室domain model可能是這個樣子的:

public class ChatRoom 
{ 
    public string Id { get; set; } 

    public string Name { get; set; } 
} 

,並且存儲過程會是這個樣子的:

CREATE PROCEDURE [dbo].[ChatRooms_Get] 
(
    @ChatRoomID VARCHAR(100), 
    @ChatRoomName VARCHAR(50) 
) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    SELECT 
      ChatRoomID, 
      ChatRoomName 
    FROM 
      tblChatRoom 
    WHERE 
      ChatRoomID = @ChatRoomID 
      AND ChatRoomName = @ChatRoomName; 
END 

GO 

然後在調用方法,你會得到的聊天室,做無線無論你需要如何處理它。對於這個例子,我只是檢查它是否存在與否:

try 
{ 
    ChatRoom chatRoom = await chatRoomRepository.GetAsync(chatRoomId, chatRoomName); 
    if (chatRoom != null) 
    { 
      MessageBox.Show("Record found"); 
    } 
    else 
    { 
      MessageBox.Show("No record found"); 
    } 
} 
catch (Exception exception) 
{ 
    throw exception; 
} 

我希望這可以提供幫助。