Showing posts with label write. Show all posts
Showing posts with label write. Show all posts

Monday, March 19, 2012

change data table from system.data into sql query

i someone had teach me how to write a query in datatable. however i need to get the data out from my database rather than the data table. can someone teach me how should i do it?

esp at the first like... like DataTable dt = GetFilledTable()

since i already have set of data in my preset table i should be getting data from SqlDataSource1 right ( however i am writing this in my background code or within <script></script>

so can anyone help me?

protected void lnkRadius_Click(object sender, EventArgs e)

{

DataTable dt = GetFilledTable();



double radius = Convert.ToDouble(txtRadius.Text);

decimal checkX = (decimal)dt.Rows[0]["Latitude"];

decimal checkY = (decimal)dt.Rows[0]["Longitude"];



// expect dt[0] to pass - as this is our check point

// We use for rather than fopreach because the later does not allow DELETE during loop execution

for(int index=0; index < dt.Rows.Count; index++)

{

DataRow dr = dt.Rows[index];



decimal testX = (decimal)dr["Latitude"];

decimal testY = (decimal)dr["Longitude"];



double testXzeroed = Convert.ToDouble(testX -= checkX);

double testYzeroed = Convert.ToDouble(testY -= checkY);



double distance = Math.Sqrt((testXzeroed * testXzeroed) + (testYzeroed * testYzeroed));



// mark for delete (not allowed in a foreach - so we use "for")

if (distance > radius)

dr.Delete();

}



// accept deletes

dt.AcceptChanges();



GridView1.DataSource = dt.DefaultView;

GridView1.DataBind();

}

should start with

SqlDataSource1.SelectCommand = "SELECT [id], [title], [largeimage], [imageTakenAt], [imageLon], [imageLat], [notes] FROM [images]";

SqlDataSource1.DataBind;

or something like that?

|||

Hi,

I would suggest you start from learning ADO.NET. You will learn how to write a query with SqlCommand, and use SqlDataAdapter to fill a DataTable.

Please start from the following link:

http://msdn2.microsoft.com/en-us/data/aa937699.aspx

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

Friday, February 24, 2012

Ceate Database Question

Hi,
I am trying to write an installtion sql script that creates a database.
However, I am running into problems because the potential of sql servers
having different setups (log and db files on different partitions...). How
do I handle this?
ThanksHow do you plan to run the script? I have used a batch file that calls
a different creation script for each possible environment. The batch
file handles the branching based on a command line parameter. So the
admin just has to type: "DBSETUP TEST" or "DBSETUP STAGE" the DOS
prompt.
If you want to make the file locations, sizes, etc totally configurable
by a non-DBA type user then I think you'll have to create a
user-interface of some kind.
David Portas
SQL Server MVP
--|||What exactly is the problem? Did you receive any error messages when you
executed the script? Can you post your script here? With CREATE DATABASE
statements, you can include the physical location of the database & log
files irrespective of the partition they are on.
Anith|||David,
Sorry I have been out of town. Is there a way to find out how the SQL
server is configured and change the script accordingly?
Thanks
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1129309258.498992.121140@.g49g2000cwa.googlegroups.com...
> How do you plan to run the script? I have used a batch file that calls
> a different creation script for each possible environment. The batch
> file handles the branching based on a command line parameter. So the
> admin just has to type: "DBSETUP TEST" or "DBSETUP STAGE" the DOS
> prompt.
> If you want to make the file locations, sizes, etc totally configurable
> by a non-DBA type user then I think you'll have to create a
> user-interface of some kind.
> --
> David Portas
> SQL Server MVP
> --
>

Thursday, February 16, 2012

Catching a General Exception

I am trying to write a query that I only want to run on sql server 2005 databases. If a server isn't 2005, it will throw an exception. I would like to catch this general exception. Here is the query...

DECLARE @.Server [nchar] (100)
SET @.Server = (CONVERT(char(100), (SELECT SERVERPROPERTY('Servername'))))

INSERT INTO [tempdb].[dbo].[User_Auditing] (Server, UserName, WinAuth, SQL_Auth_UserName, PassPolicyOn)
SELECT @.Server, s.name, isntuser, q.name, is_policy_checked
FROM sys.syslogins s FULL OUTER JOIN sys.sql_logins q
ON (s.name = q.name)

The errors I would get are as follows...

Msg 208, Level 16, State 1, Line 4
Invalid object name 'sys.syslogins'.
Msg 208, Level 16, State 1, Line 4
Invalid object name 'sys.sql_logins'.

I know in Java, I would just put a try before the declare and a catch("Invalid object name") after the statement, however, I'm not sure if this is even possible in T-SQL. Thanks for any help.
-Kyle

Nope. You cannot. 2005 introduces the concept of try...catch, but it wouldn't catch this error.

You could do something like

if @.@.version like 'Microsoft SQL Server 2005%'

exec ('select ''this is 2005''')

else

exec ('select ''this is NOT 2005''')

I used @.@.version since it will work on any version of SQL Server. Using serverproperty() is another possibilty.

Tuesday, February 14, 2012

Catch Date Gaps in Different Records

I need to write a procedure to check if I have any gaps in my dates...

Start Date.........End Date
10/6/2004.........10/6/2005
10/6/2003.........10/6/2004
7/10/2003........10/6/2003
7/10/2002..........7/10/2003

What I need to do is test the End Date to the next row under the Start Date Column. (in bold to clarify) (moving upward from bottom)

sSQL011$ = "SELECT * FROM TableName " & _
"WHERE ID = '" & ID & "'"
Set rs011 = DB.OpenRecordset(sSQL011$, dbOpenDynaset)
Set Data1.Recordset = rs011
rs011.FindFirst "ID = " & ID

With rs011
do while .eof = False

if .eof then
exit sub
end if

if rs011("StartDate") 'from one record, if it is not equal to the Start
Date in the next record then
MsgBox "Gap in Dates for: " + ID

loop

end with



pseudo...
if the end date in one consecutive row is not the same as the start date in the next row then there is a date gap

thanks...moving to sql server forum|||What database engine are you using (MS-SQL, Jet, Oracle, other)?

Are multiple active rows possible? For example, is it possible to have:Start End
1900-01-01 1925-01-01
1910-02-25 1930-02-16
1925-01-01 1950-01-01
1930-06-15 1980-07-04
1960-03-17 2000-01-01If you could have that, would there be gaps or no gaps in the coverage?

-PatP|||Records in SQL Server have no assumed order unless you specify it.

There is no such thing as "consecutive row" in SQL Server.

There is no such thing as "next row" in SQL Server.

How are you sorting your records? Please do not say: "I am sorting them by the date and time they were entered, though this is not stored anywhere in the table."