Question:
how do i connect c# with sql server 2008?
Monty D
2009-12-20 08:35:09 UTC
i have made below program using c#
using System;
class a10
{
static void Main(string[] args)
{
string com;
int c=0;
string[] make=new string[20];
string[] model=new string[20];
int[] year=new int[20];
int[] salesprice=new int[20];
Console.WriteLine("Welcome to Mullet Joe's Gently Used Autos!");
Console.WriteLine("enter comand");
Console.WriteLine("add");
Console.WriteLine("list");
Console.WriteLine("quit");
com=Convert.ToString(Console.ReadLine());
asd:
{
switch(com)
{
case "add":
Console.Write("make ");
make[c]=Convert.ToString(Console.ReadLine());
Console.Write("model ");
model[c]=Convert.ToString(Console.ReadLine());
Console.Write("year ");
year[c]=Convert.ToInt32(Console.ReadLine());
Console.Write("sales price ($) ");
salesprice[c]=Convert.ToInt32(Console.ReadLine());
c++;
goto next;
case "list":
for(int a=0;aConsole.WriteLine(year[a]+" "+make[a]+" "+model[a]+"\t"+salesprice[a]);
goto next;
case "quit":
break;
default:
Console.WriteLine("Sorry, but"+ com +"is not a valid command. Please try again.");
goto next;
next :
{
Console.WriteLine("enter comand");
Console.WriteLine("add");
Console.WriteLine("list");
Console.WriteLine("quit");
com=Convert.ToString(Console.ReadLine());
goto asd;
}
}
}
}
}
whenever i enter quit option my enter data was automatically delete
i want this program connect to sql server for to save that data
Three answers:
2009-12-20 09:17:28 UTC
The best overview is at MSDN "Retrieving and Modifying data in ado .net". http://msdn.microsoft.com/en-us/library/ms254937.aspx



ADO .NET is the technology to interact with data in a sql 2008 database.



You need a SQLconnection object, a command object. you execute a command which may return no data, a scalar or a dataset. A specific example is here

http://msdn.microsoft.com/en-us/library/tyy0sz6b.aspx



If you want to go state of the art, try LINQ in VS2008 to access data. Either way, knowing the basic ado .net objects and methods never hurts. enjoy.
Shariq (http://coinsindia.info)
2009-12-20 09:59:09 UTC
I assume that you have SQL Server is installed and configured





using System.Data.SqlClient;



static void Main(string[] args)

{



SqlConnection con = new SqlConnection();

con.ConnectionString=" "; //Your connection i.e Server Name, Database Name, Username , Password



try

{

con.Open();

SqlCommand cmd=new SqlCommand("insert into yourtable(field1,field2,field3) values(var1,var2,va3)",con);

cmd.EndExecuteNonQuery();

con.Close();



}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

con.Close();

}
?
2016-10-31 17:14:54 UTC
if u choose for to create a database, on your seen studio u can create a sq. archives document, regardless of in case you dont have sq. server put in. create a sq. datafile (.mdf extension) and connect with it. in case you have sq. show put in you could connect utilising .sqlexpress. (you need to refer gadget.archives.sq....)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...