📄 parameterscommand.cs
字号:
using System;
using System.Data;
using System.Data.OleDb;
namespace ParametersCommandApp
{
class ParametersCommand
{
static void Main()
{
OleDbConnection connection = new OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\C#Program\C#100\Chapter3\NWIND.mdb");
// create OleDbCommand objects
OleDbCommand nonqueryCommand = connection.CreateCommand();
try
{
// open connection
connection.Open();
// execute non-query to create temporary table
nonqueryCommand.CommandText =
"CREATE TABLE MyTempTable(Name VARCHAR(30),TestNumber integer)";
Console.WriteLine(nonqueryCommand.CommandText);
nonqueryCommand.ExecuteNonQuery();
// create INSERT command with named parameters
nonqueryCommand.CommandText =
"INSERT INTO MyTempTable VALUES (@Name, @Number)" ;
// add parameters to Command object Parameters collection
nonqueryCommand.Parameters.Add("@Name", OleDbType.VarChar, 30);
nonqueryCommand.Parameters.Add("@Number", OleDbType.Integer);
// prepare INSERT command for repeated execution
nonqueryCommand.Prepare();
// data to be inserted
string[] names = { "Enrico", "Franco", "Gloria", "Horace" } ;
int i;
for (i=1; i<=4; i++)
{
nonqueryCommand.Parameters["@Name"].Value = names[i-1];
nonqueryCommand.Parameters["@Number"].Value = i;
Console.WriteLine(nonqueryCommand.CommandText);
Console.WriteLine("Number of Rows Affected is: {0}",
nonqueryCommand.ExecuteNonQuery() );
}
}
catch (OleDbException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
connection.Close(); // close connection
Console.WriteLine("Connection Closed.");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -