为什么从winform插入到本地database2.mdf的文本框中获取我的数据?

时间:2018-03-31 23:18:56

标签: c# insert database-connection connection-string

我已经创建小应用程序用于打印条形码和我写插入数据代码到我的Database2.mdf.Here是我的app.config连接字符串,并用表表(型号,Sasija,Trziste)我的数据库2.mfg。 app.config

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace Barcode_ver1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dateTimePicker1.Value = DateTime.Today;
    }


    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        string barcode = textBox1.Text;


        Bitmap bitmap;
        if (textBox1.Text.Length == 0) {
            bitmap = new Bitmap(1, 100);
        } else
        {
            bitmap = new Bitmap(barcode.Length * 60, 110);
        }

        /*if (textBox1.Text.Length == 3)
        {
            textBox1.Text += "       ";
            textBox1.SelectionStart = textBox1.Text.Length;
        }*/
        using (Graphics graphics = Graphics.FromImage(bitmap))

        {
            Font oFont = new System.Drawing.Font("IDAutomationHC39M Free         Version", 12);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush White = new SolidBrush(Color.White);
            graphics.FillRectangle(White, 0, 0, bitmap.Width,   bitmap.Height);
            graphics.DrawString("*" + barcode + "*", oFont, black, point);

        }
        using (MemoryStream ms = new MemoryStream())
        {
            bitmap.Save(ms, ImageFormat.Png);
            pictureBox1.Image = bitmap;
            pictureBox1.Height = bitmap.Height;
            pictureBox1.Width = bitmap.Width;
        }
        if (textBox1.Text.Length == 3)
            textBox3.Focus();
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        string barcode = textBox2.Text;

        Bitmap bitmap;
        if (textBox2.Text.Length == 0) {
            bitmap = new Bitmap(1, 100);
        } else {
            bitmap = new Bitmap(barcode.Length * 60, 110);
        }
        using (Graphics graphics = Graphics.FromImage(bitmap))

        {
            Font oFont = new System.Drawing.Font("IDAutomationHC39M Free Version", 12);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush White = new SolidBrush(Color.White);
            graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("*" + barcode + "*", oFont, black, point);

        }
        using (MemoryStream ms = new MemoryStream())
        {
            bitmap.Save(ms, ImageFormat.Png);
            pictureBox2.Image = bitmap;
            pictureBox2.Height = bitmap.Height;
            pictureBox2.Width = bitmap.Width;
        }
      }

    Bitmap bmp;

    private void btnPrint_Click(object sender, EventArgs e)
    {
        string connetionString = null;

        connetionString = @"Data Source=(LocalDB)\MSSQLLocalDB;     AttachDbFilename=C:\Users\NIKOPOL\Documents\Database2.mdf;Integrated Security=True; Connect Timeout=30";

        SqlConnection cnn = new SqlConnection(connetionString);

        try
        {
            SqlCommand cmd = new SqlCommand();
        cmd.Connection = cnn;

        cmd.CommandText = ("INSERT into Table VALUES (@Model,@Sasija,@Trziste)");

        cmd.Parameters.AddWithValue("@Model", textBox1.Text);
        cmd.Parameters.AddWithValue("@Sasija", textBox3.Text);
        cmd.Parameters.AddWithValue("@STrziste", textBox2.Text);


            cnn.Open();
            cmd.ExecuteNonQuery();
                        }

        catch (Exception ex)
        {
            MessageBox.Show("Nije moguce otvoriti konekciju");
        }
        finally
        {
            cnn.Close();
        }


        if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length==0)
        {
            MessageBox.Show("Morate uneti polja", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        if (printDialog1.ShowDialog() == DialogResult.OK) ;
        {
            printDocument1.Print();
        }
        textBox1.Clear();
        textBox3.Clear();
        textBox2.Clear();
        textBox1.Focus();
    }


    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }


    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        //e.Graphics.DrawImage(bmp, 0, 0);
        e.Graphics.DrawImage(pictureBox1.Image, 170, 70, pictureBox1.Width, pictureBox1.Height);
        e.Graphics.DrawImage(pictureBox2.Image, 20, 185, pictureBox2.Width, pictureBox2.Height);
        Font oFont = new System.Drawing.Font("Arial", 12);
        e.Graphics.DrawString(label1.Text, oFont, Brushes.Black, 20, 75);            
        e.Graphics.DrawString(textBox1.Text, oFont, Brushes.Black, 20, 105);

        e.Graphics.DrawString(label3.Text, oFont, Brushes.Black, 80, 75);
        e.Graphics.DrawString(textBox3.Text, oFont, Brushes.Black, 80, 105);

        e.Graphics.DrawString(label2.Text, oFont, Brushes.Black, 20, 145);
        e.Graphics.DrawString(textBox2.Text, oFont, Brushes.Black, 20, 165);


        Font titleFont = new System.Drawing.Font("Arial", 20);
        e.Graphics.DrawString(label4.Text, titleFont, Brushes.Black, 76, 20);
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        string barcode = textBox1.Text + textBox3.Text;


        Bitmap bitmap;
        if (textBox3.Text.Length == 0)
        {
            bitmap = new Bitmap(1, 100);
        }
        else
        {
            bitmap = new Bitmap(barcode.Length * 60, 110);
        }

        /*if (textBox1.Text.Length == 3)
        {
            textBox1.Text += "       ";
            textBox1.SelectionStart = textBox1.Text.Length;
        }*/
        using (Graphics graphics = Graphics.FromImage(bitmap))

        {
            Font oFont = new System.Drawing.Font("IDAutomationHC39M Free Version", 12);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush White = new SolidBrush(Color.White);
            graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("*" + barcode + "*", oFont, black, point);

        }
        using (MemoryStream ms = new MemoryStream())
        {
            bitmap.Save(ms, ImageFormat.Png);
            pictureBox1.Image = bitmap;
            pictureBox1.Height = bitmap.Height;
            pictureBox1.Width = bitmap.Width;
        }
        if (textBox3.Text.Length == 8)
            textBox2.Focus();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        textBox1.Focus();
    }

它向我展示了消息" Nije moguce uspostaviti konekciju"来自catch(Exception ex)。 有人能帮我吗? 我是编码和c#的新手,但我正在尽我所能。

1 个答案:

答案 0 :(得分:-1)

我想你的ConnectionString是错误的。

  1. 转到服务器资源管理器:
  2. Server Explorer

    1. 右键点击>礼仪:
    2. Proprieties

      1. 复制连接字符串:
      2. Copy

        1. 示例:
        2.   

          使用SqlConnection。

              SqlConnection con;
              con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MyDataBase;Integrated Security=True");
          
            

          插入示例

          private void btnAdd_Click(object sender, EventArgs e)
              {
                  con.Open();
                  string Query = "insert into Professeur values ("+ txtID.Text +", '" + txtNom.Text + "', '" + txtPrenom.Text + "', '" + dtNaiss.Text + 
                      "', '"+ txtEmail.Text +"', '"+ txtPass.Text +"', '" + dtAff.Text + "', '" + cbEtat.Text + "', "+ txtNum.Text +", " + cbLycee.Text + ")";
                  cmd = new SqlCommand(Query, con);
                  cmd.ExecuteNonQuery();
                  con.Close();
              }
          
            

          更新示例

          private void btnEdit_Click(object sender, EventArgs e)
              {
                  con.Open();
                  string Query = "UPDATE Professeur SET nom = '" + txtNom.Text + "', prenom = '" + txtPrenom.Text +
                      "', dateN = '" + dtNaiss.Text + "', email = '" + txtEmail.Text + "', pass = '"+ txtPass.Text +"' , dateAffLycee = '" + dtAff.Text +"' , nEfants = '" +txtNum.Text+
                      "', idLycee = '" + cbLycee.Text + "' WHERE idProfesseur = " + txtID.Text;
                  cmd = new SqlCommand(Query, con);
                  cmd.ExecuteNonQuery();
                  con.Close();
              }
          
            

          删除示例

          private void btnDelete_Click(object sender, EventArgs e)
              {
                  con.Open();
                  string Query = "DELETE FROM Professeur WHERE idProfesseur =" + txtID.Text;
                  cmd = new SqlCommand(Query, con);
                  cmd.ExecuteNonQuery();
                  con.Close();
              }
          
            

          从DataBase到DataGrid查看示例

          con.Open();
          string Query = "SELECT Professeur.idProfesseur, nom, prenom FROM Professeur, Demande WHERE idLycee = " + cbLycee.Text + " AND Demande.idProfesseur = Professeur.idProfesseur";
          //SqlDataAdapter
          da = new SqlDataAdapter(Query, con);
          //Data Set
          ds = new DataSet();
          da.Fill(ds);
          //DataGridView
          dgv_Lycee.DataSource = ds.Tables[0];
          con.Close();