用c#做个简单知识库管理系统,连接数据库,visual studio2015能打开并运行,两天完成

2025-03-10 11:21:34
推荐回答(2个)
回答1:

帮你做了个,请采纳

添加附件失败,只有贴代码了

private void btn_Add_Click(object sender, EventArgs e)
        {
            KnowLedge kk = new KnowLedge();
            kk.Subject = cb_Subject.Text;
            kk.Know = rtb_Know.Text;
            kk.PicturePath = pic_Know.ImageLocation;
            AddNewKnowLedge(kk);
            Form1_Load(null,null);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“知识库.Knowledge”中。您可以根据需要移动或删除它。
            this.knowledgeTableAdapter.Fill(this.知识库.Knowledge);

        }

        public OleDbConnection getConnection()
        {
            OleDbConnection conn = null;
            try
            {
                string connectionstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb;Persist Security Info=True";
                conn = new OleDbConnection(connectionstr);
                conn.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return conn;
        }
        public void closeconnection(OleDbConnection conn)
        {
            try
            {
                conn.Close();
            }
            catch (Exception e)
            {

            }
        }

        public class KnowLedge
        {
            public string Subject = "";
            public string Know = "";
            public string PicturePath = "";
        }

        public void AddNewKnowLedge(KnowLedge kk)
        {
            byte[] myByte = new byte[0];
            try
            {
                FileStream filestream = new FileStream(this.pic_Know.ImageLocation, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(filestream);
                myByte = new byte[filestream.Length];
                binaryReader.Read(myByte, 0, Convert.ToInt32(filestream.Length));
                filestream.Close();
            }
            catch { }
            OleDbConnection conn = getConnection();
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = @"insert into Knowledge(Subject,Know,Picture)values('" + kk.Subject + "','" + kk.Know + "',@photo1)";
            command.CommandType = CommandType.Text;
            OleDbParameter imageType = new OleDbParameter("@photo1", OleDbType.VarBinary);
            imageType.Value = myByte;
            command.Parameters.Add(imageType).Value = myByte;
            try
            {
                command.ExecuteNonQuery();
                MessageBox.Show("添加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "添加失败");
            }
            closeconnection(conn);
        }
       
        private void pic_Know_Click(object sender, EventArgs e)
        {
            OpenFileDialog _dialog = new OpenFileDialog();
            _dialog.Filter =
            "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|" +
            "Windows Bitmap(*.bmp)|*.bmp|" +
            "Windows Icon(*.ico)|*.ico|" +
            "Graphics Interchange Format (*.gif)|(*.gif)|" +
            "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|" +
            "Portable Network Graphics (*.png)|*.png|" +
            "Tag Image File Format (*.tif)|*.tif;*.tiff";
            if (DialogResult.OK == _dialog.ShowDialog(this))
            {
                pic_Know.ImageLocation = _dialog.FileName;
            }
        }

        private void dgv_mes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //pic_Know.Image=Convert.to(dgv_mes.CurrentRow.Cells["Picture"]);
            string id = dgv_mes.CurrentRow.Cells["Col_ID"].Value.ToString();
            string sql = "select * from Knowledge where ID=" + int.Parse(id);
            DataSet ds = new DataSet();
            OleDbConnection conn = getConnection();
            OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
            oda.Fill(ds);
            byte[] img = new byte[0];
            if (ds != null)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr["Picture"].ToString().Equals(""))
                {
                    img = (byte[])dr["Picture"];
                    try
                    {
                        MemoryStream stream = new MemoryStream(img, true);
                        stream.Write(img, 0, img.Length);
                        pic_Know.Image = new Bitmap(stream);
                    }
                    catch { }
                }
                if (!dr["Know"].ToString().Equals(""))
                {
                    rtb_Know.Text = dr["Know"].ToString();
                }
                if (!dr["Subject"].ToString().Equals(""))
                {
                    cb_Subject.Text = dr["Subject"].ToString();
                }
            }
            closeconnection(conn);
        }

        private void btn_Find_Click(object sender, EventArgs e)
        {
            string sql = "select * from Knowledge where Know like '%" + rtb_Know.Text + "%' and Subject like '%" + cb_Subject.Text + "%' ";
            DataSet ds = new DataSet();
            OleDbConnection conn = getConnection();
            OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
            oda.Fill(ds);
            dgv_mes.DataSource=ds.Tables[0];
        }

回答2:

有 偿 可 做