Home > database >  C # how random data from SQL server database to array?
C # how random data from SQL server database to array?

Time:09-16

For c # to do course design, a blank, the first post, shivering

CodePudding user response:

CodePudding user response:

I write so, said an error index beyond array bounds, strives for the great god

CodePudding user response:

CodePudding user response:

Give a similar example, you can change according to the:
 using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
Using System. The Data. SqlClient.
using System.Data;
Using System.Data.Com mon;

The namespace ConsoleApplication12
{
Class Program
{
The static void Main (string [] args)
{
String connString=@ "Data Source=. \ sqlserver2005; Initial Catalog=master; Integrated Security=True ";
String SQL=@ "SELECT TOP 3 number FROM [master] dbo. Spt_values AS sv WHERE sv. [type]='P' ORDER BY NEWID ()".
DataTable dt=new DataTable();
Try
{
Using (SqlConnection conn=new SqlConnection (connString))
{
Conn. The Open ();
SqlCommand CMD=new SqlCommand (SQL, conn);
SqlDataAdapter adapter=new SqlDataAdapter (CMD);
Adapter. The Fill (dt);
Console. WriteLine (" implementation success, ");
}

Console. WriteLine (" numerical output: ");
Foreach (DataRow Dr In dt. Rows)
{
Console. WriteLine (Dr [0]. ToString ());
}
}
The catch (Exception ex)
{
Console. WriteLine (" failure: "+ ex. Message);
}

The Console. The Read ();
}
}
}

CodePudding user response:

reference 4 floor yenange response:
give a similar example, you can change according to the:
 using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
Using System. The Data. SqlClient.
using System.Data;
Using System.Data.Com mon;

The namespace ConsoleApplication12
{
Class Program
{
The static void Main (string [] args)
{
String connString=@ "Data Source=. \ sqlserver2005; Initial Catalog=master; Integrated Security=True ";
String SQL=@ "SELECT TOP 3 number FROM [master] dbo. Spt_values AS sv WHERE sv. [type]='P' ORDER BY NEWID ()".
DataTable dt=new DataTable();
Try
{
Using (SqlConnection conn=new SqlConnection (connString))
{
Conn. The Open ();
SqlCommand CMD=new SqlCommand (SQL, conn);
SqlDataAdapter adapter=new SqlDataAdapter (CMD);
Adapter. The Fill (dt);
Console. WriteLine (" implementation success, ");
}

Console. WriteLine (" numerical output: ");
Foreach (DataRow Dr In dt. Rows)
{
Console. WriteLine (Dr [0]. ToString ());
}
}
The catch (Exception ex)
{
Console. WriteLine (" failure: "+ ex. Message);
}

The Console. The Read ();
}
}
}


Thank you, problem has been solved, I write so

Is not directly with dt. Rows [I] [0]. The ToString (); I put its value is assigned to the label. The Text was successful

CodePudding user response:

reference 5 floor tiandaosi12 reply:
thanks, problem has been solved, I write so

Is not directly with dt. Rows [I] [0]. The ToString (); I put it in the value assigned to the label. The Text was successful


What are you doing to be fixed?
I write good code, you have to do is to change the connection string and SQL?

Don't be stubborn in their own ideas, beginner program, see somebody else's mature code,

CodePudding user response:

Is the code to copy copy to her

CodePudding user response:

Learning is always need to have an idea can reproduce the don't understand what's the use
  • Related