Program
    {
        static void Main(string[] args)
        {
            OperateAccess();
        }

        
public static void OperateAccess()
        {
            
// OLE DB.NET Framework 数据提供程序使用标有问号 (?) 的定位参数,而不使用命名参数。
            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\sharetovm\db1.mdb;User Id=admin;Password=;";
            
string insertSQL = "INSERT INTO info([name],[address]) values(?,?)";
            
using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                
// The insertSQL string contains a SQL statement that
                
// inserts a new row in the source table.
                OleDbCommand command = new OleDbCommand(insertSQL);
                
//access数据库用问号占位符的时候,添加的值是按问号的先后顺序的
                command.Parameters.Add("@name", OleDbType.VarChar, 50"name").Value = "Jhon";
                command.Parameters.Add(
"@address", OleDbType.VarChar, 50"address").Value = "China";

                
// Set the Connection to the new OleDbConnection.
                command.Connection = connection;
                
try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                
catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
// The connection is automatically closed when the
                
// code exits the using block.
            }
        }
    }

 

 

 C#使用参数操作access数据库

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2021-11-28
  • 2022-12-23
  • 2022-01-30
猜你喜欢
  • 2021-11-18
  • 2022-12-23
  • 2021-12-27
  • 2021-10-24
  • 2021-07-26
相关资源
相似解决方案