- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
今天我一直坐在我的电脑前尝试如何连接和使用 MySql 数据库。我有很多问题,但我会尽量只问几个问题。
当我使用语句 sqlConnection.Open();
初始化与我的服务器的连接时此连接现在是否已打开并准备好使用,直到我以其他方式通知服务器?
Close()
语句似乎没有关闭连接,如果我查看我的服务器状态,我可以看到发送后连接仍然存在。
假设我想从我的数据库中检索数据,我首先执行我的函数 setupConnection();
此函数发送 Open()
然后返回 true 或 false。然后我可能会有一个函数来检索数据以进行显示和计算等。当我调用这个函数时,我是否需要再次打开连接?
总结一下,连接是否只有在程序与 Open();
函数处于同一作用域时才打开?
还有我该怎么做才能不必在每个函数中都声明它:
MySqlConnection 连接;sqlConnection = new MySqlConnection();
这是我今天一直在做的一些代码:
/// <summary>
/// InitConnection outputs the connectionstring
/// </summary>
/// <param name="Adress">Server adress</param>
/// <param name="Port">Server port</param>
/// <param name="Uid">Username</param>
/// <param name="Pwd">Password</param>
/// <param name="Database">Database</param>
/// <returns>the connectionstring</returns>
public string initConnection(string Adress, string Port,
string Uid, string Pwd, string Database)
{
return "server=" + Adress + ";port=" + Port + ";uid=" + Uid + ";" +
"pwd=" + Pwd + ";database=" + Database + ";";
}
/// <summary>
/// setuupConnection will setup an active connection to the database
/// specified in initConnection
/// </summary>
/// <param name="ConnectionString">The return value of
/// initConnection</param>
/// <returns>True or False</returns>
public bool setupConnection(string ConnectionString)
{
MySqlConnection sqlConnection;
sqlConnection = new MySqlConnection();
sqlConnection.ConnectionString = ConnectionString;
try
{
sqlConnection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server.");
break;
case 1045:
MessageBox.Show("Invalide username/password.");
break;
}
return false;
}
}
/// <summary>
/// closeConnection will terminate the database connection.
/// </summary>
/// <returns>True or False</returns>
public bool closeConnection()
{
MySqlConnection sqlConnection;
sqlConnection = new MySqlConnection();
try
{
sqlConnection.Dispose();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
最佳答案
您正在设置和关闭功能范围内设置您的连接。你设置你的连接,打开它,然后你的功能结束并且连接超出范围。在您的关闭功能中,您定义的连接只是为了处理它,这是没有意义的。设置您的连接,打开它,使用它,然后关闭它。
看来您应该研究和理解变量作用域。
关于c# - 我不太明白 MySql 连接是什么样的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778826/
在我们的服务出现一些预期的增长之后,突然间一些更新花费了非常长的时间,这些过去非常快,直到表达到大约 2MM 记录,现在它们每个需要大约 40-60 秒。 update table1 set fiel
我在服务中实现了一个传感器事件监听器,只要采样周期和最大报告延迟低于 1 秒,该监听器就可以正常工作,但一旦我将采样周期增加到超过 1 秒,传感器就根本不会更新。 我希望采样周期为 10 秒(可能是
我使用 Tkinter GUI 来启动测量和分析过程,基本上只需单击一个按钮即可开始。由于这些测量可能需要一段时间,我尝试添加一个进度条,即这个: http://tkinter.unpythonic.
我正在尝试使用套接字发送数据包,但出现错误。 invalid conversion from ‘omnetpp::cPacket*’ to ‘inet::Packet*’ [-fpermissive]
我刚刚发现 String#split 有以下奇怪的行为: "a\tb c\nd".split => ["a", "b", "c", "d"] "a\tb c\nd".split(' ') => ["a
您好,我正在尝试 ClojureScript,我正在使用 Klipse作为我的 REPL 差不多。这可能不是它的预期用途,但因为我没有做任何太复杂的事情,所以现在没问题。 我遇到的一个问题是尝试设置计
根据下面的数据,ClockKit 会生成一次 future 的 CLKComplicationTimelineEntry 项,但对于过去的时间点,会进行 24 次调用!这是为什么? 更多详情: 我注意
我有一个 MySQL 表,这个表有一个名为 datetime_utc 的 DATETIME 列。如您所料,它是 UTC 日期和时间。在我的 Bookshelf 模型中,我定义了一个虚拟 getter,
大家好,我是二哥呀! 昨天,一位球友问我能不能给他解释一下 @SpringBootApplication 注解是什么意思,还有 Spring Boot 的运行原理,于是我就带着他扒拉了一下这个注解的源
我是一名优秀的程序员,十分优秀!