我遇到与谷歌云SQL服务器连接的问题。
当我在这个C#代码中执行查询SELECT * FROM Countries;
时:
private SQLConnection()
{
StringBuilder s = new StringBuilder();
s.Append("Server=" + ConfigurationManager.AppSettings["SQLHost"] + ';');
s.Append("userid=" + ConfigurationManager.AppSettings["SQLUser"] + ';');
s.Append("password=" + ConfigurationManager.AppSettings["SQLPassword"] + ';');
s.Append("database=" + ConfigurationManager.AppSettings["DBName"] + ';');
try
{
connection = new MySqlConnection(s.ToString());
connection.Open();
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
try
{
string stm = s.ToString();
MySqlCommand cmd = new MySqlCommand(stm + ";", connection);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
data.AddLast(rdr.GetString(0) + ": "
+ rdr.GetString(1) + ": " + rdr.GetString(2));
}
}
第一个s.tostring()用于连接建立。第二个s.toString()用于查询构建。
我得到一个空洞的答案。
但是在我的python代码中,当我在:
中执行相同的查询时try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM Countries"
cursor.execute(sql)
result = cursor.fetchone()
print(result)
finally:
connection.close()
我得到了我想要的完整答案。任何想法?