Jump to content

C# database

- - - - -

  • Please log in to reply
104 replies to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i have a next problem...i have application from which i have to connect to my database...i made database in c#...and i don't know how to connect it...i need to save information into table from user input on my form by clicking button...after that i need to create some form from which i can see all informations in table and to have a button to print that table...
here is my code for saving informations but it doesn't work
Code:
private void button1_Click_2(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection())
{


string ConnectionString = "Data Source=localhost;Initial Catalog=ETSDTB;Integrated Security=SSPI;";
SqlConnection myConnection = "Trusted_Connection=yes;" +
"database=ETSDTB;" +
"connection timeout=30";

myConnection.Open();


try
{
myConnection.Open();
}
catch (Exception)
{
MessageBox.Show(ToString());
}
SqlParameter Ime = new SqlParameter("@Ime", SqlDbType.VarChar, 11);

SqlCommand myCommand = new SqlCommand("INSERT into Tehničar_za_računalstvo (Ime, Prezime, Dan_Rođenja, Mjesec_Rođenja, Godina_Rođenja, Učeni_Jezik, Religijski_Predmet, Dodatni_Bodovi, Broj_Dod.Bodova, Matematika_7, Matematika_8, Hrvatski_7, Hrvatski_8, Strani_Jezik_7, Strani_Jezik_8, Fizika_7, Fizika_8, Teh._Kultura_7, Teh._Kultura_8, Prosjek_7, Ponavljač)"+
"VALUES (@textBox22_TextChanged,@textBox12_TextChanged_2,@textBox13_TextChanged_1,@textBox19_TextChanged_2,@textBox20_TextChanged_1,@textBox11_TextChanged_2,@textBox3_TextChanged_1,@textBox16_TextChanged_2,@textBox18_TextChanged_2,@textBox17_TextChanged_2,@textBox4_TextChanged_1,@textBox6_TextChanged_1,@textBox5_TextChanged_1,@textBox8_TextChanged_1,@textBox7_TextChanged_1,@textBox10_TextChanged_1,@textBox9_TextChanged_1,@textBox15_TextChanged_2,@textBox14_TextChanged_2,@textBox2_TextChanged_1,@textBox23_TextChanged", myConnection);

}

}
please give me code which you have checked...i've tried a lot of things but i didn't find a solution

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
I'm not going to touch the code, but just looking at this,
your database server is localhost, are you sure it isnt a SQL Server Express instance? By default it's named,

so that would be
Data Source=localhost\SQLExpress;Initial Catalog=ETSDTB;Integrated Security=SSPI;

I would create it like this

using (SqlConnection connection = new SqlConnection("Data Source=localhost\SQLExpress;Initial Catalog=ETSDTB;Integrated Security=SSPI;") {

    connection.Open();

     using (SqlCommand command = new SqlCommand("SQL Query", command) {

       //Your code here, like command.ExecuteNonQuery();

     }

}



#3
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
can i put in

using (SqlCommand command = new SqlCommand("SQL Query", command) {
//Your code here, like command.ExecuteNonQuery();
}

sql code like insert into Tehničar_za_računalstvo(column1,...) values(textBox1,...)
???

#4
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i've changed System.Data.SqlServer namespace into System.Data.SqlServerCe because i'm using .sdf format of database...and i've used classes from that namespace...after i've tested my program i go to visual c#(2010) and checked my database (which i've created in Visual C#) and there were no more my table and it said

Quote

The event log file is full


#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Yea, in Sql Query, I mean your query, like
INSERT INTO table ( field, field1 ) VALUES ( @field, @field1 );

I'm having a hard time following exactly what you're explaining. (I think we have just a slight language barrier)
But if you're saying what I think you are, you need to change your connection string somewhat

You should set your data source to the path of the file. examples:
SQL Server Compact Edition Connection String Samples - ConnectionStrings.com

Also,
try clearing your event log (Control Panel->Administrative Tools->Event Viewer
Locate the event log (on the left hand side), right click it and hit "Clear all Events"

#6
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i have finished that...now i want to sort my data in rows in my table...for example i have indefinite number of people in my table...i want to sort them by their sales...first in table should be the one who have the biggest sale, and the last one should be the one who have the smallest sale...do i have to write my own code in c# or in sql???
can you post a code for that example???

#7
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
yea, thats a SQL query thing. Is the sales colum in the same table?
That can be accomplished like this:

SELECT E.* FROM employees E ORDER BY E.sales DESC;


if you need to calculate the sales, you can do that too... id honestly need to see how your database is laid out, but there are a few options.

#8
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
it's not sale for real...it's just example...for real i need to sort students by their mark

#9
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
firstly i need to make sum of 5 columns (which present their marks) and put that sum into special column...after that i need to sort them by their sum of marks...sorry if speak english bad, i'm from croatia

#10
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
I wouldn't worry about your English, it isn't as bad as you think. =)

so, lets assume you have 3 tables. students, subjects and grades

students: id, name
subjects: id, name
grades: student_id, subject_id, grade

for the sake of argument, lets say you have two students:
1, Sammy Samsam
2, John Doe

and you have 3 subjects
1, Math
2, English
3, Physics

and lastly, we assign some grades for the students
1,1,75
1,2,80
1,3,60
2,1,95
2,2,60
2,3,90

If you have any experience with relational databases, you may have realized that the student_id and subject_id fields are foreign keys. So the grade: 1,1,75 , would translate to Sammy Samsam, Math, 75

now the magic...

SELECT

	ST.name 'Student Name',

	Avg(G.grade) 'Grade Average'

FROM

	grades G

	INNER JOIN students ST ON G.student_id = ST.id

	INNER JOIN subjects SU ON G.subject_id = SU.id

GROUP BY

	ST.name

ORDER BY

	'Grade Average' DESC


note that you can reverse the order, by changing DESC to ASC


Why not give it a try?

#11
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
sorry but i'm new with data bases...can you explain me what means that query that you write

#12
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
and if i'm not mistaking you didn't write how i can make sum of values of 5 columns and to put their result of sum into 6th column
i know how to do it in c and c# but i don't know how to do it in sql




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users