취향이 msdn 상품에 DataBind
속성.
ASP 예
void Page_Load(Object sender, EventArgs e)
{
// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the
// page is first loaded. Thereafter, the values are
// stored in view state.
if(!IsPostBack)
{
// Declare the query string.
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
// Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
}
}
DataSet GetData(String queryString)
{
// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch(Exception ex)
{
// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";
}
return ds;
}
이 조각 (a)는 DataBind 메서드를 사용하여 할당함으로써,있는 gridview에 데이터 집합을 바인딩하는 방법을 보여줍니다.
는
이 사이트는 말한다 :
가 GridView 컨트롤에 데이터 소스에서 데이터를 바인딩 할 데이터 바인딩() 메서드를 사용합니다. 이 메서드는 컨트롤의 활성 템플릿에있는 모든 데이터 바인딩 식을 확인합니다.
해결 내 말은 선을 참조하고 싶은
: 실제로 이 컨트롤에 데이터를 결합
AuthorsGridView.DataBind();
.
SIDE 참고 SQLI로부터 자신을 보호하기 위해
, 당신은 직접 연결을 SQL Parameters에 읽어 및 안된다.
의 WinForm 예
튜토리얼이 발견되었다 또한 here
//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb";
//create the database query
string query = "SELECT * FROM MyTable";
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
:
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
이 줄을 제거하십시오 dataGridView1.DataSource = ""; 그리고 우리가 당신을 도울 수 있도록 더 자세한 내용을 포함 .... –
내 로그인 이름은 ... ... MrSQLInjection); DROP TABLE LibraryInfo; -'P.s. 내 이름을 사용하지 마십시오;) – Reniuz
OP가 누락 된 유일한 부분은'dataGridView1.DataBind(); '입니다. –