我正在将一条记录添加到远程mysql数据库中,之后我想检查它是否已添加-所以我想尝试选择这个特定的记录。
我的代码:
public void Select(string filename)
{
string query = "SELECT * FROM banners WHERE file = '"+filename+"'";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
如何检查服务器的响应?还是选定的记录?它是c#windows窗体应用程序。
您可以通过扩展插入语句
SELECT LAST_INSERT_ID();
执行插入命令
command.ExecureQuery();
并获取最后插入的记录的ID。
看看MySQL信息函数
试试这个:
public void Select(string filename)
{
string query = "SELECT * FROM banners WHERE file = '"+filename+"'";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Get the DataReader from the comment using ExecuteReader
MySqlDataReader reader = cmd.ExecuteReader();
while (myReader.Read())
{
//Use GetString etc depending on the column datatypes.
Console.WriteLine(myReader.GetInt32(0));
}
//close connection
this.CloseConnection();
}
}
有关详细信息,请查看此URL:http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.100).aspx
您可以在为INSERT
语句本身运行command.ExecuteNonQuery()
时执行此操作。command.ExecuteNonQuery()
返回受影响的行数(在您的案例中插入)。所以
if(command.ExecuteNonQuery()>0)
{
//inserted
}
else
{
//Not inserted
}
http://dev.mysql.com/doc/refman/5.1/en/connector-net-ref-mysqlclient.html#connector-net ref mysqlclient mysqlcommand executenoquery