Gå til innhold

vfr

Medlemmer
  • Innlegg

    4
  • Ble med

  • Besøkte siden sist

Innlegg skrevet av vfr

  1. Når jeg skriver inn tekst i txtNavn og sender den til datagridview, står det "System.Windows.Forms. TexBoks, Text og det jeg skrev. Se Bilde.

     

    Her er hele sql koden.

      

    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.Data.SqlClient;
     
     
    namespace DB_System
    {
        public partial class Form1 : Form
        {
            SqlConnection connection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Kjetil\Documents\Visual Studio 2017\Projects\DB_System\DB_System\DB_Server1.mdf;Integrated Security = True; Connect Timeout = 30");
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
     
            private void btnInsert_Click(object sender, EventArgs e)
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into[MyTable] (Name,Surname,Address) values ('" + txtName + "','" + txtSurname + "','" + txtAddress + "')";
                cmd.ExecuteNonQuery();
                connection.Close();
                txtName.Text = "";
                txtSurname.Text = "";
                txtAddress.Text = "";
                txtSearch.Text = "";
                display_data();
                MessageBox.Show("Data inserted Successfully");
            }
            //To display data
            public void display_data()
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from [MyTable]";
                cmd.ExecuteNonQuery();
                DataTable dta = new DataTable();
                SqlDataAdapter dataadp = new SqlDataAdapter(cmd);
                dataadp.Fill(dta);
                dataGridView1.DataSource = dta;
                connection.Close();
            }
     
            private void btnDisplay_Click(object sender, EventArgs e)
            {
                display_data();
            }
     
            private void btnDelete_Click(object sender, EventArgs e)
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "delete from [MyTable] where name = '" + txtName.Text + "'";
                cmd.ExecuteNonQuery();
                connection.Close();
                txtName.Text = "";
                txtSurname.Text = "";
                txtAddress.Text = "";
                txtSearch.Text = "";
                display_data();
                MessageBox.Show("Data deleated Successfully");
            }
     
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "update [MyTable] set name = '" + txtName.Text + "'where name ='" + txtSurname.Text + "'"; ;
                cmd.ExecuteNonQuery();
                connection.Close();
                txtName.Text = "";
                txtSurname.Text = "";
                txtAddress.Text = "";
                txtSearch.Text = "";
                display_data();
                MessageBox.Show("Data updatet Successfully");
            }
     
            private void btnSearch_Click(object sender, EventArgs e)
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from [MyTable] where name = '" + txtSearch.Text + "'";
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                connection.Close();
                txtName.Text = "";
                txtSurname.Text = "";
                txtAddress.Text = "";
                txtSearch.Text = "";
                
            }
        }
    }
     

     

  2. Denne koden fungerer helt til jeg skal telle opp antall partall og oddetall.

     

     

    import itertools

     

    f= open('testrekker.txt','w')

    numb = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

    it = itertools.combinations(numb,7)

     

    #summerer tall rekka.

    for x in it:

        a = sum(x)

     

    #teller oddetall og partall

    numbers = x

    count_even = 0

    count_odd = 0

    for i in numbers:

        if not i % 2:

            count_even+=1

        else:

             count_odd+=1    

     

    #skriver resultattet til tekstfil

        f.write(str(x))

        f.write(str(a))

        f.write(str(count_even))

        f.write(str(count_odd))

        f.write('\n')

    f.close()

     

     

    Resultatet blir: 


    (6, 7, 8, 9, 10, 11, 12)6310

    (6, 7, 8, 9, 10, 11, 12)6311

    (6, 7, 8, 9, 10, 11, 12)6321

    (6, 7, 8, 9, 10, 11, 12)6322

    (6, 7, 8, 9, 10, 11, 12)6332

    (6, 7, 8, 9, 10, 11, 12)6333

    (6, 7, 8, 9, 10, 11, 12)6343


     

    Skulle vært:

    (1,2,3,4,5,6,7) 63, 3, 4

    osv.... 

×
×
  • Opprett ny...