A moze li se napraviti da recimo napravim neki textBox i da unesem broj, npr, 3 i da 3 random unosa on ubaci sam u bazu podataka? ako ja recimo odaberem dva datuma(od-do, sa date pickera) a sam da random izabere neki datum/vreme odatle, dogadjaj i korisnika?
ja sam stavio format ovako
Code:
dtpOd.Format = DateTimePickerFormat.Custom;
dtpOd.CustomFormat = "ddddd, MMMM dd, yyyy hh:mm:ss tt";
Ali ne vidim u tvom kodu gde je deo koji upisuje i vreme u bazu? jer ja sam stavio format da pokazuje i vreme u datapicker, ali u mom kodu stavlja u bazu samo datum, a ne vreme.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
namespace zadatak2
{
public partial class Form1 : Form
{
private OleDbConnection connection=new OleDbConnection();
public Form1()
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VS2010 projects\zadatak2\zadatak2\fedek.accdb;Jet OLEDB:Database Password=fedek;";
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dtpOd.Format = DateTimePickerFormat.Custom;
dtpOd.CustomFormat = "ddddd, MMMM dd, yyyy hh:mm:ss tt";
dtpDo.Format = DateTimePickerFormat.Custom;
dtpDo.CustomFormat = "ddddd, MMMM dd, yyyy hh:mm:ss tt";
connection.Open();
string query="SELECT korisnik from korisnici";
string query2 = "select dogadjaj from dogadjaji";
OleDbCommand command = new OleDbCommand(query, connection);
command.Connection = connection;
OleDbDataReader reader = command.ExecuteReader();
while(reader.Read())
{
comboBox1.Items.Add(reader["korisnik"].ToString());
}
command = new OleDbCommand(query2, connection);
reader = command.ExecuteReader();
while (reader.Read())
{
comboBox2.Items.Add(reader["dogadjaj"].ToString());
}
connection.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
string query = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + this.dtpOd.Value.Date + "','" + this.dtpDo.Value.Date + "') ;";
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
command1.CommandText = query;
OleDbDataReader reader;
try
{
connection.Open();
reader = command1.ExecuteReader();
MessageBox.Show("Saved");
while (reader.Read())
{
}
}
catch (Exception ex1)
{
MessageBox.Show("error" + ex1);
}
connection.Close();
}
private void button1_Click(object sender, EventArgs e)
{
new Random((int)DateTime.Now.Ticks);
int staro1 = comboBox1.SelectedIndex;
int staro2 = comboBox2.SelectedIndex;
comboBox1.SelectedIndex = (new Random()).Next(comboBox1.Items.Count);
comboBox2.SelectedIndex = (new Random()).Next(comboBox2.Items.Count);
string tekstKomande = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + this.dtpOd.Value.Date + "','" + this.dtpDo.Value.Date + "') ;";
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
command1.CommandText = tekstKomande;
OleDbDataReader reader;
try
{
connection.Open();
reader = command1.ExecuteReader();
MessageBox.Show("saved");
while (reader.Read())
{
}
}
catch (Exception ex1)
{
MessageBox.Show("error" + ex1);
}
connection.Close();
comboBox1.SelectedIndex = staro1;
comboBox2.SelectedIndex = staro2;
}
}
}
[Ovu poruku je menjao tomislav91 dana 04.10.2014. u 20:00 GMT+1]