如何将访问数据库与c#应用程序链接 [英] How to link an access db with a c# application

查看:73
本文介绍了如何将访问数据库与c#应用程序链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#创建了一个表单。我希望与我创建的访问数据库建立直接连接,即如果我输入assetID,应用程序应检索与该ID对应的行关联的所有数据,并在数据库中自动更新单击按钮的日期。有人能为我提供一个小代码吗?

I have created a form using c#. I wish to have a direct connection with an access db that I have created i.e. If I enter assetID , the app should retrieve all data associated with rows corresponding to that ID and automatically update in the db the date when the button is clicked . Would someone be able to provide me a small code for the same?

推荐答案





下面代码将在标签中显示资产名称,其ID与文本框相同值





Hi,

Below Code Will Show Asset Name in Label which ID same as textbox Value


private void btnSubmit_Click(object sender, EventArgs e)
        {
            string filePath =@"D:\DB\Database1.mdb";
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath); 
            OleDbCommand cmd = new OleDbCommand("select * from TblAsset where AssetID="+txtAssetID.Text);
            con.Open();
           
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                lblAssetName.Text = dr["AssetName"].ToString();
            }
            dr.Close();
            dr.Dispose();
            con.Close();

        }







谢谢

Siva Rm K




Thanks
Siva Rm K


CP帮助人们学习。我们既不做他们的作业/家庭作业/项目也不提供源代码。

你尝试了什么 [ ^ ]

如果您的编码遇到任何困难,总是先咨询谷歌,其他一切都失败了,然后访问CP并提出与特定问题相关的问题。

尽管如此,既然你在这里,你可能想学习并适应以下链接:

step-逐步连接到访问数据库-c-sharp [ ^ ]

使用Microsoft Access的C#中的简单电影数据库 [ ^ ]
CP helps people to learn. We neither do their assignments/homeworks/projects nor supply source code.
what-have-you-tried[^]
If you encounter any difficulties with your coding, always consult Google first, everything else fails, then visit CP and ask questions related to specific issues.
Nevertheless, since you are here, you may want to learn and adapt from the following links:
step-by-step-connect-to-access-database-in-c-sharp[^]
Simple Movie Database in C# using Microsoft Access[^]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {OleDbConnection mycon = new OleDbConnection();
	       mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1.accdb";
           OleDbCommand command = new OleDbCommand();
           command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
           mycon.Open();
           command.Connection = mycon;
           command.ExecuteNonQuery();
           mycon.Close();
            
            
        }
    }
}


这篇关于如何将访问数据库与c#应用程序链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆