本文整理汇总了C#中MySqlCommand类的典型用法代码示例。如果您正苦于以下问题:C# MySqlCommand类的具体用法?C# MySqlCommand怎么用?C# MySqlCommand使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MySqlCommand类属于命名空间,在下文中一共展示了MySqlCommand类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: button1_Click
private void button1_Click(object sender, EventArgs e)
{
try
{
MySqlConnection myConn = new MySqlConnection();
myConn.Host = "192.168.69.8";
myConn.Port = 3306;
myConn.UserId = "cody";
myConn.Password = "greenstreetelite";
myConn.Open();
MySqlCommand myCommand = new MySqlCommand(" SELECT * FROM registration.regUser ;", myConn);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = myCommand;
DataTable dbDataSet = new DataTable();
myDataAdapter.Fill(dbDataSet);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbDataSet;
dataGridView1.DataSource = bSource;
myDataAdapter.Update(dbDataSet);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例2: MySqlConnection
public int fjöldi_rada()
{
MySqlConnection connection;
string connectionString = "Server=10.0.105.33;Database=Leikur;Uid=first;Pwd=first;";
connection = new MySqlConnection(connectionString);
connection.Open();
string query = @"SELECT * FROM spilari";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
MySqlDataReader queryCommandReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(queryCommandReader);
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds, "SQL Temp Table");
adapter.Dispose();
cmd.Dispose();
return ds.Tables[0].Rows.Count;
}
示例3: PossibleValues
public void PossibleValues()
{
if (Version < new Version(5, 0)) return;
execSQL(@"CREATE PROCEDURE spTest (id INT UNSIGNED ZEROFILL,
dec1 DECIMAL(10,2),
name VARCHAR(20) /* this is a comment */ CHARACTER SET ascii,
t1 TINYTEXT BINARY, t2 ENUM('a','b','c'),
t3 /* comment */ SET(/* comment */'1','2','3'))
BEGIN SELECT name; END");
MySqlCommand cmd = new MySqlCommand("spTest", conn);
cmd.CommandType = CommandType.StoredProcedure;
//MySqlCommandBuilder.DeriveParameters(cmd);
Assert.IsNull(cmd.Parameters["@id"].PossibleValues);
Assert.IsNull(cmd.Parameters["@dec1"].PossibleValues);
Assert.IsNull(cmd.Parameters["@name"].PossibleValues);
Assert.IsNull(cmd.Parameters["@t1"].PossibleValues);
MySqlParameter t2 = cmd.Parameters["@t2"];
Assert.IsNotNull(t2.PossibleValues);
Assert.AreEqual("a", t2.PossibleValues[0]);
Assert.AreEqual("b", t2.PossibleValues[1]);
Assert.AreEqual("c", t2.PossibleValues[2]);
MySqlParameter t3 = cmd.Parameters["@t3"];
Assert.IsNotNull(t3.PossibleValues);
Assert.AreEqual("1", t3.PossibleValues[0]);
Assert.AreEqual("2", t3.PossibleValues[1]);
Assert.AreEqual("3", t3.PossibleValues[2]);
}
示例4: EscapedBackslash
public void EscapedBackslash()
{
execSQL("CREATE TABLE Test(id INT, name VARCHAR(20))");
MySqlCommand cmd = new MySqlCommand(@"INSERT INTO Test VALUES (1, '\\=\\')", conn);
cmd.ExecuteNonQuery();
}
示例5: MySqlReader
public MySqlReader(MySqlCommand command)
{
if (command.Type == MySqlCommandType.SELECT)
{
TryFill(command);
}
}
示例6: Blobs
public void Blobs()
{
execSQL("CREATE TABLE Test (id INT, blob1 LONGBLOB, text1 LONGTEXT)");
MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES (?id, ?blob1, ?text1)", conn);
byte[] bytes = Utils.CreateBlob(400000);
string inStr = "This is my text";
cmd.Parameters.AddWithValue("?id", 1);
cmd.Parameters.AddWithValue("?blob1", bytes);
cmd.Parameters.AddWithValue("?text1", inStr);
cmd.Prepare();
int count = cmd.ExecuteNonQuery();
Assert.AreEqual(1, count);
cmd.CommandText = "SELECT * FROM Test";
cmd.Prepare();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
Assert.IsTrue(reader.Read());
Assert.AreEqual(1, reader.GetInt32(0));
Assert.AreEqual(bytes.Length, reader.GetBytes(1, 0, null, 0, 0));
byte[] outBytes = new byte[bytes.Length];
reader.GetBytes(1, 0, outBytes, 0, bytes.Length);
for (int x = 0; x < bytes.Length; x++)
Assert.AreEqual(bytes[x], outBytes[x]);
Assert.AreEqual(inStr, reader.GetString(2));
}
}
示例7: ExecuteReader
public void ExecuteReader()
{
if (Version < new Version(5, 0)) return;
execSQL("CREATE TABLE test (id int)");
execSQL("CREATE PROCEDURE spTest() BEGIN INSERT INTO test VALUES(1); " +
"SELECT SLEEP(2); SELECT 'done'; END");
MySqlCommand proc = new MySqlCommand("spTest", conn);
proc.CommandType = CommandType.StoredProcedure;
IAsyncResult iar = proc.BeginExecuteReader();
int count = 0;
while (!iar.IsCompleted)
{
count++;
System.Threading.Thread.Sleep(20);
}
using (MySqlDataReader reader = proc.EndExecuteReader(iar))
{
Assert.IsNotNull(reader);
Assert.IsTrue(count > 0, "count > 0");
Assert.IsTrue(reader.Read(), "can read");
Assert.IsTrue(reader.NextResult());
Assert.IsTrue(reader.Read());
Assert.AreEqual("done", reader.GetString(0));
reader.Close();
proc.CommandType = CommandType.Text;
proc.CommandText = "SELECT COUNT(*) FROM test";
object cnt = proc.ExecuteScalar();
Assert.AreEqual(1, cnt);
}
}
示例8: ProblemCharsInSQLUTF8
public void ProblemCharsInSQLUTF8()
{
if (Version < new Version(4, 1)) return;
execSQL("CREATE TABLE Test (id INT NOT NULL, name VARCHAR(250), mt MEDIUMTEXT, " +
"PRIMARY KEY(id)) CHAR SET utf8");
using (MySqlConnection c = new MySqlConnection(GetConnectionString(true) + ";charset=utf8"))
{
c.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES (?id, ?text, ?mt)", c);
cmd.Parameters.AddWithValue("?id", 1);
cmd.Parameters.AddWithValue("?text", "This is my;test ? string–’‘’“”…");
cmd.Parameters.AddWithValue("?mt", "My MT string: ?");
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT * FROM Test";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
Assert.IsTrue(reader.Read());
Assert.AreEqual(1, reader.GetInt32(0));
Assert.AreEqual("This is my;test ? string–’‘’“”…", reader.GetString(1));
Assert.AreEqual("My MT string: ?", reader.GetString(2));
}
}
}
示例9: AggregateTypesTest
public void AggregateTypesTest()
{
execSQL("CREATE TABLE foo (abigint bigint, aint int)");
execSQL("INSERT INTO foo VALUES (1, 2)");
execSQL("INSERT INTO foo VALUES (2, 3)");
execSQL("INSERT INTO foo VALUES (3, 4)");
execSQL("INSERT INTO foo VALUES (3, 5)");
// Try a normal query
string NORMAL_QRY = "SELECT abigint, aint FROM foo WHERE abigint = {0}";
string qry = String.Format(NORMAL_QRY, 3);
MySqlCommand cmd = new MySqlCommand(qry, conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
reader.GetInt64(0);
reader.GetInt32(1); // <--- aint... this succeeds
}
}
cmd.CommandText = "SELECT abigint, max(aint) FROM foo GROUP BY abigint";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
reader.GetInt64(0);
reader.GetInt64(1); // <--- max(aint)... this fails
}
}
}
示例10: BadIndexUsed
public void BadIndexUsed()
{
execSQL("DROP TABLE IF EXISTS Test");
execSQL("CREATE TABLE Test(id INT, name VARCHAR(20) PRIMARY KEY)");
execSQL("INSERT INTO Test VALUES (1, 'Test1')");
execSQL("INSERT INTO Test VALUES (2, 'Test2')");
execSQL("INSERT INTO Test VALUES (3, 'Test3')");
execSQL("INSERT INTO Test VALUES (4, 'Test4')");
MySqlTrace.Listeners.Clear();
MySqlTrace.Switch.Level = SourceLevels.All;
GenericListener listener = new GenericListener();
MySqlTrace.Listeners.Add(listener);
MySqlCommand cmd = new MySqlCommand("SELECT name FROM Test WHERE id=3", conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
}
Assert.AreEqual(6, listener.Strings.Count);
Assert.IsTrue(listener.Strings[0].Contains("Query Opened: SELECT name FROM Test WHERE id=3"));
Assert.IsTrue(listener.Strings[1].Contains("Resultset Opened: field(s) = 1, affected rows = -1, inserted id = -1"));
Assert.IsTrue(listener.Strings[2].Contains("Usage Advisor Warning: Query does not use an index"));
Assert.IsTrue(listener.Strings[3].Contains("Usage Advisor Warning: The following columns were not accessed: name"));
Assert.IsTrue(listener.Strings[4].Contains("Resultset Closed. Total rows=1, skipped rows=0, size (bytes)=6"));
Assert.IsTrue(listener.Strings[5].Contains("Query Closed"));
}
示例11: Warnings
public void Warnings()
{
if (version < new Version(4, 1)) return;
conn.InfoMessage += new MySqlInfoMessageEventHandler(WarningsInfoMessage);
execSQL("DROP TABLE IF EXISTS test");
execSQL("CREATE TABLE test (name VARCHAR(10))");
MySqlCommand cmd = new MySqlCommand("INSERT INTO test VALUES ('12345678901')", conn);
MySqlDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
finally
{
if (reader != null)
reader.Close();
}
}
示例12: TransactionScopeInternal
void TransactionScopeInternal(bool commit)
{
MySqlConnection c = new MySqlConnection(GetConnectionString(true));
MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES ('a', 'name', 'name2')", c);
try
{
using (TransactionScope ts = new TransactionScope())
{
c.Open();
cmd.ExecuteNonQuery();
if (commit)
ts.Complete();
}
cmd.CommandText = "SELECT COUNT(*) FROM Test";
object count = cmd.ExecuteScalar();
Assert.AreEqual(commit ? 1 : 0, count);
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
finally
{
if (c != null)
{
c.Close();
}
}
}
示例13: CommitAfterConnectionDead
public void CommitAfterConnectionDead()
{
execSQL("DROP TABLE IF EXISTS Test");
execSQL("CREATE TABLE Test(id INT, name VARCHAR(20))");
string connStr = GetConnectionString(true) + ";pooling=false";
using (MySqlConnection c = new MySqlConnection(connStr))
{
c.Open();
MySqlTransaction trans = c.BeginTransaction();
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES (1, 'boo')", c))
{
cmd.ExecuteNonQuery();
}
KillConnection(c);
try
{
trans.Commit();
Assert.Fail("Should have thrown an exception");
}
catch (Exception ex)
{
}
Assert.AreEqual(ConnectionState.Closed, c.State);
c.Close(); // this should work even though we are closed
}
}
示例14: Simple
public void Simple()
{
execSQL("DROP TABLE IF EXISTS Test");
execSQL("CREATE TABLE Test (id INT, dec1 DECIMAL(5,2), name VARCHAR(100))");
execSQL("INSERT INTO Test VALUES (1, 345.12, 'abcd')");
MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES(1,345.12,'abcd')", conn);
cmd.Prepare();
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT * FROM Test";
cmd.Prepare();
MySqlDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
Assert.IsTrue(reader.Read());
Assert.AreEqual(1, reader.GetInt32(0));
Assert.AreEqual(345.12, reader.GetDecimal(1));
Assert.AreEqual("abcd", reader.GetString(2));
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
finally
{
if (reader != null) reader.Close();
}
}
示例15: Bug51610
public void Bug51610()
{
MySqlCommand cmd = new MySqlCommand("SELECT 'ABC', (0/`QOH`) from (SELECT 1 as `QOH`) `d1`", conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
Assert.AreEqual("ABC", reader.GetString(0));
Assert.AreEqual(0, reader.GetInt32(1));
}
cmd.CommandText = "SELECT 'ABC', (0-`QOH`) from (SELECT 1 as `QOH`) `d1`";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
Assert.AreEqual("ABC", reader.GetString(0));
Assert.AreEqual(-1, reader.GetInt32(1));
}
cmd.CommandText = "SELECT 'test 2010-03-04 @ 10:14'";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
Assert.AreEqual("test 2010-03-04 @ 10:14", reader.GetString(0));
}
}