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 ChatRoomID=@ChatRoomID,ChatRoomName=@ChatRoomName 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);
}
}
在编译程序时,我在数据库中只得到连接失败消息框 我发现正确,如何克服程序获取连接成功消息框
答案 0 :(得分:9)
好吧,你正在填写ds
数据集 - 但是你正在检查dt
数据表中是否存在行......当然,它永远不会起作用!
如果您只需要一个DataTable
- 只需单独使用和填充该数据表 - 无需DataSet
的开销。另外,将SqlConnection
和SqlCommand
放入使用这样的块:
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
中没有行并不一定意味着你的连接失败了.....可能只是没有符合你的搜索标准的行!连接工作得很好 - 但SQL命令只是没有返回任何行。
答案 1 :(得分:0)
连接失败意味着您的程序和数据库之间出现了问题。没有返回记录并不意味着连接失败。它只是意味着你的表是空的 - 它不包含任何记录。
使用ADO.NET
和stored 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;
}
我希望这可以提供帮助。